文件输入与输出
文件 std::File 本身实现了 Read 和 Write trait,所以文件的输入输出非常简单,只要得到一个 File 类型实例就可以调用读写接口进行文件输入与输出操作了。而要得到 File 就得让操作系统打开(open)或新建(create)一个文件。还是拿例子来说明
1. use std::io;
2. use std::io::prelude::*;
3. use std::fs::File;
5. // create file and write something
6. fn create_file(filename: &str, buf: &[u8]) -> io::Result<()> {
7. let mut f = try!(File::create(filename));
8. try!(f.write(&buf));
9. Ok(())
10. }
12. // read from file to String
13. fn read_file(filename: &str, buf: &mut String) -> io::Result<()> {
14. let mut f = try!(File::open(filename));
15. try!(f.read_to_string(&buf));
16. Ok(())
17. }
19. fn main() {
20. let f = "foo.txt";
21. let mut buf = String::new();
22. match create_file(f, b"Hello, World!") {
23. Ok(()) => {
24. match read_file(f, &mut buf) {
25. Ok(()) => {println!("{}", buf);},
26. Err(e) => {println!("{}", e);},
27. };
28. },
29. Err(e) => {println!("{}", e);},
30. }
31. }
文件操作上面 Rust 与其它语言处理方式有些不一样,其它语言一般把读写选项作为函数参数传给 open 函数,而 Rust 则是在 option 上面调用 open 函数。 std::OpenOptions 是一个 builder,通过 new 函数创建后,可以链式调用设置打开文件的选项,是 read, write, append, truncate 还是 create 等,OpenOptions 构建完成后就可以再接着调用 open 方法了,看下下面的例子就明白了
1. use std::fs::OpenOptions;
3. let file = OpenOptions::new().write(true).truncate(true).open("foo.txt");
Rust 这种用 builder pattern 来设置打开文件选项,相比于将选项以字符作为参数传给 open 函数的一个优点是可以让编译器保证检查选项合法性,不用等到运行时才发现手抖把 read-mode 的 r 写成了 t。
