Rust 10.01 std_fs模块
2023-02-05 10:10:59    30    0    0
admin

use std::fs;

https://doc.rust-lang.org/std/fs/index.html

  1. use std::fs;
  2. fn main() {
  3. //fs::create_dir("/tmp/lala/121").unwrap();//mkdir
  4. fs::create_dir_all("/tmp/lala/121").unwrap();//mkdir -p
  5. let text = fs::read_to_string("./src/main.rs").expect("Read file failed!");//读取文件
  6. println!("{}",text);
  7. fs::write("/tmp/abc.rs",text);
  8. fs::rename("/tmp/abc.rs", "/tmp/aaa.rs").unwrap();
  9. for the_file in fs::read_dir("/tmp").unwrap() {
  10. println!("{:?}",the_file.unwrap().path().to_str().unwrap());
  11. }
  12. }

https://doc.rust-lang.org/std/fs/struct.OpenOptions.html
https://doc.rust-lang.org/std/io/trait.Write.html

  1. use std::fs::OpenOptions;
  2. use std::io::Write;
  3. fn main() {
  4. let mut file_hd = OpenOptions::new()
  5. .write(true)
  6. .create(true)
  7. .append(true)
  8. .open("/tmp/11111.txt")
  9. .unwrap();
  10. file_hd.write(b"xxxxxxxxx\n").unwrap();
  11. let s = "121\n".to_string();
  12. file_hd.write(s.as_bytes()).unwrap();
  13. let s = "qqq".to_string();
  14. file_hd.write((&s).as_bytes()).unwrap();
  15. }

上一篇: Rust 10.00 std_env模块

下一篇: Rust 10.02 std_process模块

文档导航