summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-9-error-handling/guessing-result.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/theBook/chapter-9-error-handling/guessing-result.rs')
-rw-r--r--rust/theBook/chapter-9-error-handling/guessing-result.rs31
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);
+}