Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1147 - No more open_mode method #1164

Merged
merged 1 commit into from
Mar 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/std_misc/file/create.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ already existed, the old content is destroyed. Otherwise, a new file is
created.

```rust,ignore
static LOREM_IPSUM: &'static str =
"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
static LOREM_IPSUM: &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
Expand All @@ -15,8 +15,8 @@ proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
";

use std::error::Error;
use std::io::prelude::*;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

fn main() {
Expand All @@ -25,18 +25,13 @@ fn main() {

// Open a file in write-only mode, returns `io::Result<File>`
let mut file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}",
display,
why.description()),
Err(why) => panic!("couldn't create {}: {}", display, why.description()),
Ok(file) => file,
};

// Write the `LOREM_IPSUM` string to `file`, returns `io::Result<()>`
match file.write_all(LOREM_IPSUM.as_bytes()) {
Err(why) => {
panic!("couldn't write to {}: {}", display,
why.description())
},
Err(why) => panic!("couldn't write to {}: {}", display, why.description()),
Ok(_) => println!("successfully wrote to {}", display),
}
}
Expand All @@ -60,5 +55,6 @@ 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.
There is [`OpenOptions`] struct that can be used to configure how a file is opened.

[`OpenOptions`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html