From 0e89468188b9d347495dad5b98af137e5f477432 Mon Sep 17 00:00:00 2001 From: garhve Date: Sun, 1 Jan 2023 23:23:55 +0800 Subject: finish chapter 9 --- .../chapter-9-error-handling/guessing-result.rs | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rust/theBook/chapter-9-error-handling/guessing-result.rs (limited to 'rust/theBook/chapter-9-error-handling/guessing-result.rs') 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); +} -- cgit v1.2.3-70-g09d2