【Rust】打开文件
环境
- Rust 1.56.1
- VSCode 1.61.2
概念
参考:https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/open.html
示例
main.rs
use std::fs::File;
use std::io::Read;
use std::path::Path;
fn main() {
let path = Path::new("hello.txt");
let display = path.display();
let mut file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {}", display, why),
Ok(file) => file,
};
let mut s = String::new();
match file.read_to_string(&mut s) {
Err(why) => panic!("couldn't read {}: {}", display, why),
Ok(_) => print!("{} contains:\n{}", display, s),
}
}
总结
了解了 Rust 中读取文件内容的方法。