Skip to content

Commit bbb2177

Browse files
committed
stuff
1 parent 575018a commit bbb2177

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
mod guessgame;
22
mod basics;
3+
mod myutils {
4+
pub mod stuff;
5+
}
6+
use myutils::stuff;
37

48
fn main() {
59
basics::basics();
610
println!();
11+
stuff::collections();
12+
stuff::error_handling();
13+
println!();
714
guessgame::guess_game();
815
}

src/myutils/stuff.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use core::panic;
2+
use std::{
3+
fs::File,
4+
io::{ErrorKind, Read},
5+
};
6+
7+
pub fn collections() {
8+
let v = vec![1, 2, 3, 4, 5];
9+
println!("Vector: {:?}", v);
10+
}
11+
12+
pub fn error_handling() {
13+
match read_username_from_file() {
14+
Ok(username) => println!("Username: {}", username),
15+
Err(e) => println!("Error reading username: {:?}", e),
16+
}
17+
18+
let f = File::open("hello.txt");
19+
match f {
20+
Ok(file) => file,
21+
Err(error) => match error.kind() {
22+
ErrorKind::NotFound => panic!("File not found: {:?}", error),
23+
other_error => {
24+
panic!("Fatal problem opening the file: {:?}", other_error)
25+
}
26+
},
27+
};
28+
}
29+
30+
fn read_username_from_file() -> Result<String, std::io::Error> {
31+
let mut s = String::new();
32+
File::open("hello.txt")?.read_to_string(&mut s)?;
33+
Ok(s)
34+
}

0 commit comments

Comments
 (0)