Skip to content

Commit

Permalink
Rollup merge of #136704 - benschulz:patch-1, r=ibraheemdev
Browse files Browse the repository at this point in the history
Improve examples for file locking

The `lock` and `try_lock` documentation states that "if the file not open for writing, it is unspecified whether this function returns an error." With this change, the examples use `File::create` instead of `File::open`, eliminating the possibility of someone blindly copying code with unspecified behavior.
  • Loading branch information
jhpratt authored Feb 11, 2025
2 parents aaf2c46 + 8ea20c8 commit 2996cfd
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ impl File {
/// use std::fs::File;
///
/// fn main() -> std::io::Result<()> {
/// let f = File::open("foo.txt")?;
/// let f = File::create("foo.txt")?;
/// f.lock()?;
/// Ok(())
/// }
Expand Down Expand Up @@ -767,7 +767,7 @@ impl File {
/// use std::fs::File;
///
/// fn main() -> std::io::Result<()> {
/// let f = File::open("foo.txt")?;
/// let f = File::create("foo.txt")?;
/// f.try_lock()?;
/// Ok(())
/// }
Expand Down

0 comments on commit 2996cfd

Please sign in to comment.