summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-9-error-handling/guessing-result.rs
blob: 9b6e3d259878703acecb6a222a5179b34d918baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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);
}