Rollup merge of #118623 - haydonryan:master, r=workingjubilee

Improve std::fs::read_to_string example

Resolves  [#118621](https://github.com/rust-lang/rust/issues/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

```
This commit is contained in:
Matthias Krüger 2024-03-08 08:19:16 +01:00 committed by GitHub
commit 876847bed8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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(())
/// }
/// ```
@ -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(())
/// }
/// ```