Skip to content

Commit a43b5b1

Browse files
committed
added readline example
1 parent 0d0ce84 commit a43b5b1

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ in a controlled repository now.
1313
* `closures.rs`: closure examples
1414
* `lifetimes.rs`: simple lifetime example
1515
* `rc`: examples with `RefCell` and `Rc`
16+
* `readline`: example of readline with error handling
1617
* `error`: example of creating a custom error

readline.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// Read from stdin with default.
2+
fn main() {
3+
use std::io::BufRead;
4+
let stdin = std::io::stdin();
5+
let mut stdin = stdin.lock();
6+
let mut line = String::new();
7+
match stdin.read_line(&mut line) {
8+
Ok(_) => {
9+
print!("{}", line);
10+
}
11+
Err(e) => {
12+
eprintln!("read_line: {}", e);
13+
std::process::exit(1);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)