通过例子学习Rust

46.2 create

The create static method opens a file in write-only mode. If the file already existed, the old content is destroyed, otherwise a new file is created.

use std::io::File; static LOREM_IPSUM: &'static str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. "; fn main() { let path = Path::new("out/lorem_ipsum.txt"); let display = path.display(); // Open a file in write-only mode, returns `IoResult<File>` let mut file = match File::create(&path) { Err(why) => panic!("couldn't create {}: {}", display, why.desc), Ok(file) => file, }; // Write the `LOREM_IPSUM` string to `file`, returns `IoResult<()>` match file.write_str(LOREM_IPSUM) { Err(why) => { panic!("couldn't write to {}: {}", display, why.desc) }, Ok(_) => println!("successfully wrote to {}", display), } }

As in the previous example, the playpen won't allow file I/O, so you'll hit one of the failure paths. Here's the expected successful output:

$ mkdir out
$ rustc create.rs && ./create
successfully wrote to out/lorem_ipsum.txt
$ cat out/lorem_ipsum.txt
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

(As in the previous example, you are encouraged to test this example under failure conditions)

There is also a more generic open_mode method that can open files in other modes like: read+write, append, etc.