Skip to content

Commit

Permalink
Rollup merge of #118623 - haydonryan:master, r=workingjubilee
Browse files Browse the repository at this point in the history
Improve std::fs::read_to_string example

Resolves  [#118621](#118621)

For the original code to succeed it requires address.txt to contain a socketaddress, however it is much easier to follow if this is just any strong - eg address could be a street address or just text.

Also changed the variable name from "foo" to something more meaningful as cargo clippy warns you against using foo as a placeholder.

```
$ cat main.rs
use std::fs;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let addr: String = fs::read_to_string("address.txt")?.parse()?;
    println!("{}", addr);
    Ok(())
}

$ cat address.txt
123 rusty lane
san francisco 94999

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `/home/haydon/workspace/rust-test-pr/tester/target/debug/tester`
123 rusty lane
san francisco 94999

```
  • Loading branch information
matthiaskrgr committed Mar 8, 2024
2 parents 9fb91aa + b5e1ca3 commit 876847b
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions library/std/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ pub struct DirBuilder {
///
/// ```no_run
/// use std::fs;
/// use std::net::SocketAddr;
///
/// fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
/// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
/// let data: Vec<u8> = fs::read("image.jpg")?;
/// assert_eq!(data[0..3], [0xFF, 0xD8, 0xFF]);
/// Ok(())
/// }
/// ```
Expand Down Expand Up @@ -290,11 +290,11 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
///
/// ```no_run
/// use std::fs;
/// use std::net::SocketAddr;
/// use std::error::Error;
///
/// fn main() -> Result<(), Box<dyn Error>> {
/// let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
/// let message: String = fs::read_to_string("message.txt")?;
/// println!("{}", message);
/// Ok(())
/// }
/// ```
Expand Down

0 comments on commit 876847b

Please sign in to comment.