Skip to content

Commit edce4a8

Browse files
committed
Add lang-question_mark.rs
1 parent 1a1cee0 commit edce4a8

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: rust
2-
rust: 1.4.0
2+
rust: 1.13.0
33
env:
44
global:
55
- secure: YuWDOwPGprL6PiBZVeGaLOCHHgC18fQ6nq617tlt7XFCkG17r/xDWq3iiAyNxNUcwsD9CkWi5aXok8SlX3rQx84XC/sya2bi0+8Frt9EztcVxgMwWuX3Ll4mFHO7HnMhazoQYRPd0b0w4s7bFY2WidxCqjsMKRgAM26Gn+6oQto=

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ rust-examples [![Open HUB statistics](https://www.openhub.net/p/rust-examples/wi
77
gather example codes from tutorial and other documentations of
88
[Rust](http://www.rust-lang.org/) into files, ready to compile.
99

10-
Examples are tested with version 1.3.0.
10+
Examples are tested with version 1.13.0.
1111

1212
## Files
1313

@@ -39,6 +39,7 @@ Examples are tested with version 1.3.0.
3939
* Generics, Polymorphism, by [Felix S. Klock II](https://github.com/Rust-Meetup-Paris/Talks/tree/master/introduction_to_rust): `lang-generics.rs`
4040
* Overloading by [Rust By Example](http://rustbyexample.com/ops.html): `lang-overloading.rs`
4141
* Pointer snippets from Dave Herman's talk: `lang-pointers.rs`
42+
* Question mark operator introduced in [Rust-1.13](https://blog.rust-lang.org/2016/11/10/Rust-1.13.html): `lang-question_mark.rs`
4243
* [std::collections:HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html): `api-collections-hashmap.rs`
4344
* [getopts](http://doc.rust-lang.org/getopts/getopts/index.html): `api-getopts.rs`
4445
* [std::FromStr](https://doc.rust-lang.org/core/str/trait.FromStr.htmll): `api-std-from_str.rs`

data/username.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
eliovir

lang-question_mark.rs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//! Example of the new operator `?` added in Rust-1.13.0.
2+
//! See https://blog.rust-lang.org/2016/11/10/Rust-1.13.html
3+
4+
use std::io::prelude::*;
5+
use std::fs::File;
6+
use std::io;
7+
8+
fn read_username_from_file() -> Result<String, io::Error> {
9+
let mut f = File::open("data/username.txt")?;
10+
let mut s = String::new();
11+
12+
f.read_to_string(&mut s)?;
13+
14+
Ok(s)
15+
}
16+
17+
fn read_username_from_file_with_matches() -> Result<String, io::Error> {
18+
let f = File::open("data/username.txt");
19+
let mut f = match f {
20+
Ok(file) => file,
21+
Err(e) => return Err(e),
22+
};
23+
24+
let mut s = String::new();
25+
match f.read_to_string(&mut s) {
26+
Ok(_) => Ok(s),
27+
Err(e) => Err(e),
28+
}
29+
}
30+
31+
fn read_username_from_file_with_try() -> Result<String, io::Error> {
32+
let mut f = try!(File::open("data/username.txt"));
33+
let mut s = String::new();
34+
35+
try!(f.read_to_string(&mut s));
36+
37+
Ok(s)
38+
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
#[test]
43+
fn question_mark() {
44+
let found = ::read_username_from_file();
45+
assert!(!found.is_err());
46+
assert_eq!(found.unwrap(), "eliovir\n".to_string());
47+
}
48+
#[test]
49+
fn with_matches() {
50+
let found = ::read_username_from_file_with_matches();
51+
assert!(!found.is_err());
52+
assert_eq!(found.unwrap(), "eliovir\n".to_string());
53+
}
54+
#[test]
55+
fn with_try() {
56+
let found = ::read_username_from_file_with_try();
57+
assert!(!found.is_err());
58+
assert_eq!(found.unwrap(), "eliovir\n".to_string());
59+
}
60+
}
61+
62+
fn main() {
63+
match read_username_from_file() {
64+
Ok(s) => println!("The file `data/username.txt` contains `{}`.", s),
65+
Err(e) => println!("Something went wrong: {}", e),
66+
};
67+
68+
match read_username_from_file_with_matches() {
69+
Ok(s) => println!("The file `data/username.txt` contains `{}`.", s),
70+
Err(e) => println!("Something went wrong: {}", e),
71+
};
72+
73+
match read_username_from_file_with_try() {
74+
Ok(s) => println!("The file `data/username.txt` contains `{}`.", s),
75+
Err(e) => println!("Something went wrong: {}", e),
76+
};
77+
}

0 commit comments

Comments
 (0)