use std::io::{Write, Seek, SeekFrom};
use std::io::ErrorKind;
use std::fs::{File, OpenOptions};
use std::error;
fn main() -> Result<(), Box<dyn error::Error>> {
let file_path = "./hello.txt";
let file = File::open(file_path);
let mut file = match file {
Ok(file) => file,
Err(error) => {
match error.kind() {
ErrorKind::NotFound => {
match File::create(file_path) {
Ok(f) => f,
Err(error) => panic!("Problem creating the file: {:?}", error)
}
}
other_error => panic!("Problem opening the file: {:?}", other_error)
}
}
};
// let mut file = OpenOptions::new()
// .read(true)
// .write(true)
// .append(true)
// .create(true)
// .open(file_path).unwrap();
file.write("你好中国\n".as_bytes())?;
file.flush();
Ok(())
}
解决方法:
把注释的打开,即可。