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

17 lines
358 B
Rust
Raw Normal View History

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