-
StackOverflow 文档
-
Rust 教程
-
文件 IO
-
将文件作为一个整体读取
use std::fs::File;
use std::io::Read;
fn main() {
let filename = "src/main.rs";
// Open the file in read-only mode.
match File::open(filename) {
// The file is open (no error).
Ok(mut file) => {
let mut content = String::new();
// Read all the file content into a variable (ignoring the result of the operation).
file.read_to_string(&mut content).unwrap();
println!("{}", content);
// The file is automatically closed when is goes out of scope.
},
// Error handling.
Err(error) => {
println!("Error opening file {}: {}", filename, error);
},
}
}