FS

文件 std::fs::File 本身实现了 ReadWrite trait,所以文件的输入输出非常简单,只要得到一个 File 类型实例就可以调用读写接口进行文件输入与输出操作了。而要得到 File 就得让操作系统打开(open)或新建(create)一个文件。

  1. use std::io;
  2. use std::io::prelude::*;
  3. use std::fs::File;
  4. // create file and write something
  5. fn create_file(filename: &str, buf: &[u8]) -> io::Result<()> {
  6. let mut f = try!(File::create(filename));
  7. try!(f.write(&buf));
  8. Ok(())
  9. }
  10. // read from file to String
  11. fn read_file(filename: &str, mut buf: &mut String) -> io::Result<()> {
  12. let mut f = try!(File::open(filename));
  13. try!(f.read_to_string(&mut buf));
  14. Ok(())
  15. }
  16. fn main() {
  17. let f = "foo.txt";
  18. let mut buf = String::new();
  19. match create_file(f, b"Hello, World!") {
  20. Ok(()) => {
  21. match read_file(f, &mut buf) {
  22. Ok(()) => {println!("{}", buf);},
  23. Err(e) => {println!("{}", e);},
  24. };
  25. },
  26. Err(e) => {println!("{}", e);},
  27. }
  28. }

文件操作上面 Rust 与其它语言处理方式有些不一样,其它语言一般把读写选项作为函数参数传给 open 函数,而 Rust 则是在 option 上面调用 open 函数。 std::fs::OpenOptions 是一个 builder,通过 new 函数创建后,可以链式调用设置打开文件的选项,是 read, write, append, truncate 还是 create 等,OpenOptions 构建完成后就可以再接着调用 open 方法了,看下下面的例子就明白了

  1. use std::fs::OpenOptions;
  2. let file = OpenOptions::new().write(true).truncate(true).open("foo.txt");

Rust 这种用 builder pattern 来设置打开文件选项,相比于将选项以字符作为参数传给 open 函数的一个优点是可以让编译器保证检查选项合法性,不用等到运行时才发现手抖把 read-mode 的 r 写成了 t