summaryrefslogtreecommitdiff
path: root/rust/theBook/chapter-9-error-handling
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2023-01-01 23:23:55 +0800
committergarhve <git@garhve.com>2023-01-01 23:23:55 +0800
commit0e89468188b9d347495dad5b98af137e5f477432 (patch)
tree0109b539d1defe6469099476df262838ddf02320 /rust/theBook/chapter-9-error-handling
parentbbc35c878f0f0fcf7234f23efb6820aebf4287c3 (diff)
finish chapter 9
Diffstat (limited to 'rust/theBook/chapter-9-error-handling')
-rw-r--r--rust/theBook/chapter-9-error-handling/guessing-result.rs31
-rw-r--r--rust/theBook/chapter-9-error-handling/panic/Cargo.toml8
-rw-r--r--rust/theBook/chapter-9-error-handling/panic/src/main.rs5
-rw-r--r--rust/theBook/chapter-9-error-handling/result/Cargo.toml8
-rw-r--r--rust/theBook/chapter-9-error-handling/result/src/main.rs62
5 files changed, 114 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);
+}
diff --git a/rust/theBook/chapter-9-error-handling/panic/Cargo.toml b/rust/theBook/chapter-9-error-handling/panic/Cargo.toml
new file mode 100644
index 0000000..f484c90
--- /dev/null
+++ b/rust/theBook/chapter-9-error-handling/panic/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "panic"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-9-error-handling/panic/src/main.rs b/rust/theBook/chapter-9-error-handling/panic/src/main.rs
new file mode 100644
index 0000000..a3bc2a7
--- /dev/null
+++ b/rust/theBook/chapter-9-error-handling/panic/src/main.rs
@@ -0,0 +1,5 @@
+fn main() {
+ let x = vec![1,2,3];
+
+ x[99];
+}
diff --git a/rust/theBook/chapter-9-error-handling/result/Cargo.toml b/rust/theBook/chapter-9-error-handling/result/Cargo.toml
new file mode 100644
index 0000000..e32be4b
--- /dev/null
+++ b/rust/theBook/chapter-9-error-handling/result/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "result"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/rust/theBook/chapter-9-error-handling/result/src/main.rs b/rust/theBook/chapter-9-error-handling/result/src/main.rs
new file mode 100644
index 0000000..265250c
--- /dev/null
+++ b/rust/theBook/chapter-9-error-handling/result/src/main.rs
@@ -0,0 +1,62 @@
+use std::fs::{Self,File};
+use std::io::{Self, Read, ErrorKind};
+
+fn main() {
+ let greeting_file_result = File::open("hello.txt");
+
+ let greeting_file = match greeting_file_result {
+ Ok(file) => file,
+ Err(error) => match error.kind() {
+ ErrorKind::NotFound => match File::create("hello.txt") {
+ Ok(fc) => fc,
+ Err(e) => panic!("problem opening file {:?}",e),
+ },
+ other_error => {
+ panic!("problem opening file {:?}",other_error);
+ }
+ },
+ };
+
+ // or use unwrap, it will return value or panic just like match
+ // let greeting_file = File::open("hello.txt").unwrap();
+}
+
+// functions to read username from file
+// each one is a shorter solution of its previous one.
+fn read_username_from_file() -> Result<String, io::Error> {
+ let username_file_result = File::open("hello.txt");
+
+ let username_file = match username_file_result {
+ Ok(file) => file,
+ Err(e) => return Err(e),
+ };
+
+ let mut username = String::new();
+
+ match username_file.read_to_string(&mut username) {
+ Ok(_) => Ok(username),
+ Err(e) => Err(e),
+ }
+}
+
+// ? is like match, but shorter
+// return content of Ok() if true, or return Err() from whole function
+fn read_username_from_file_short() -> Result<String, io::Error> {
+ let mut username_file = File::open("hello.txt")?;
+
+ let mut username = String::new();
+
+ username_file.read_to_string(&mut username)?;
+ Ok(username)
+}
+
+fn read_username_from_file_even_short() -> Result<String, io::Error> {
+ let mut username = String::new();
+
+ File::open("hello.txt")?.read_to_string(&mut username)?;
+ Ok(username)
+}
+
+fn read_username_from_file_shortest() -> Result<String, io::Error> {
+ fs::read_to_string("hello.txt")
+}