forked from eliovir/rust-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlang-question_mark.rs
77 lines (64 loc) · 1.84 KB
/
lang-question_mark.rs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//! Example of the new operator `?` added in Rust-1.13.0.
//! See https://blog.rust-lang.org/2016/11/10/Rust-1.13.html
use std::io::prelude::*;
use std::fs::File;
use std::io;
fn read_username_from_file() -> Result<String, io::Error> {
let mut f = File::open("data/username.txt")?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
fn read_username_from_file_with_matches() -> Result<String, io::Error> {
let f = File::open("data/username.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e),
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
}
fn read_username_from_file_with_try() -> Result<String, io::Error> {
let mut f = File::open("data/username.txt")?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
}
#[cfg(test)]
mod tests {
#[test]
fn question_mark() {
let found = ::read_username_from_file();
assert!(!found.is_err());
assert_eq!(found.unwrap(), "eliovir\n".to_string());
}
#[test]
fn with_matches() {
let found = ::read_username_from_file_with_matches();
assert!(!found.is_err());
assert_eq!(found.unwrap(), "eliovir\n".to_string());
}
#[test]
fn with_try() {
let found = ::read_username_from_file_with_try();
assert!(!found.is_err());
assert_eq!(found.unwrap(), "eliovir\n".to_string());
}
}
fn main() {
match read_username_from_file() {
Ok(s) => println!("The file `data/username.txt` contains `{}`.", s),
Err(e) => println!("Something went wrong: {}", e),
};
match read_username_from_file_with_matches() {
Ok(s) => println!("The file `data/username.txt` contains `{}`.", s),
Err(e) => println!("Something went wrong: {}", e),
};
match read_username_from_file_with_try() {
Ok(s) => println!("The file `data/username.txt` contains `{}`.", s),
Err(e) => println!("Something went wrong: {}", e),
};
}