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

16 lines
398 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_file = File::open("hello.txt")?;
let mut username = String::new();
username_file.read_to_string(&mut username)?;
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");
}