diff options
author | garhve <git@garhve.com> | 2023-01-01 23:23:55 +0800 |
---|---|---|
committer | garhve <git@garhve.com> | 2023-01-01 23:23:55 +0800 |
commit | 0e89468188b9d347495dad5b98af137e5f477432 (patch) | |
tree | 0109b539d1defe6469099476df262838ddf02320 /rust/theBook/chapter-9-error-handling/guessing-result.rs | |
parent | bbc35c878f0f0fcf7234f23efb6820aebf4287c3 (diff) |
finish chapter 9
Diffstat (limited to 'rust/theBook/chapter-9-error-handling/guessing-result.rs')
-rw-r--r-- | rust/theBook/chapter-9-error-handling/guessing-result.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/rust/theBook/chapter-9-error-handling/guessing-result.rs b/rust/theBook/chapter-9-error-handling/guessing-result.rs new file mode 100644 index 0000000..9b6e3d2 --- /dev/null +++ b/rust/theBook/chapter-9-error-handling/guessing-result.rs @@ -0,0 +1,31 @@ +#[derive(Debug)] +pub struct Guess { + value: i32, +} + +impl Guess { + pub fn new(value: i32) -> Guess { + if value < 1 || value > 100 { + panic!("the value entered should be within the range of 1 and 100"); + } + + Guess { value } // return Guess value + } + + pub fn value(&self) -> i32 { + self.value + } +} + +fn main() { + let m = Guess::new(5); + + // this is not allowed once this + // struct is defined in external module. + // The structure field is private. + let m = Guess { + value: 32, + }; + + println!("{:?}", m); +} |