trpl-zh-cn/listings/ch09-error-handling/listing-09-08/src/main.rs

17 lines
357 B
Rust
Raw Normal View History

2022-02-06 16:43:51 +08:00
// ANCHOR: here
use std::fs::File;
2023-01-16 17:34:52 +08:00
use std::io::{self, Read};
2022-02-06 16:43:51 +08:00
fn read_username_from_file() -> Result<String, io::Error> {
2023-01-16 17:34:52 +08:00
let mut username = String::new();
2022-02-06 16:43:51 +08:00
2023-01-16 17:34:52 +08:00
File::open("hello.txt")?.read_to_string(&mut username)?;
2022-02-06 16:43:51 +08:00
2023-01-16 17:34:52 +08:00
Ok(username)
2022-02-06 16:43:51 +08:00
}
// ANCHOR_END: here
fn main() {
let username = read_username_from_file().expect("Unable to get username");
}