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

Function std::fs::read_to_string example code fails to run on command line and needs improving. #118621

Closed
haydonryan opened this issue Dec 4, 2023 · 2 comments · Fixed by #118623
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@haydonryan
Copy link
Contributor

Location

https://doc.rust-lang.org/std/fs/fn.read_to_string.html

cargo 1.74.0 (ecb9851af 2023-10-18)
rustc 1.74.0 (79e9716 2023-11-13)

Summary

I'm still very new to rust, but as part of advent of code this year found that the example for std::fs::read_to_string fails. I was also surprised to see the return value being a socket address not a string. I created a simple test program and the sample code returns an error. It would be much better having this return a string and print it out.

2124 $ cat main.rs 
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()?;
    Ok(())
}

2125 $ cat address.txt
123 rusty lane
san francisco 94999

2126 $ cargo run
warning: unused variable: `foo`
 --> src/main.rs:6:9
  |
6 |     let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
  |         ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
  |
  = note: `#[warn(unused_variables)]` on by default

warning: `tester` (bin "tester") generated 1 warning (run `cargo fix --bin "tester"` to apply 1 suggestion)
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `/home/haydon/workspace/rust-test-pr/tester/target/debug/tester`
Error: AddrParseError(Socket)

I'm submitting a PR shortly and would love some feedback on it.

@haydonryan haydonryan added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label Dec 4, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 4, 2023
@aDotInTheVoid aDotInTheVoid added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 5, 2023
@jdahlstrom
Copy link

jdahlstrom commented Dec 10, 2023

The example uses the parse method to parse into a SocketAddr -- note that it's read_to_string()?.parse()?, not just read_to_string()?! The read_to_string function itself returns a String as expected. A SocketAddr is an IP address and port number, so if you put something like 192.168.0.123:4567 into address.txt, the example should work fine.

Now, I do think that the example makes matters needlessly confusing by introducing the SocketAddr type and requiring the reader to have familiarity with how parse works, both of which are just red herrings. Particularly given that parse uses return type inference, plus delegation to the FromStr trait, to be able to parse a string into a number of different types.

@haydonryan
Copy link
Contributor Author

Thankyou!

I've updated this in the PR and also added a second instance of using socketaddr for fs::read.

workingjubilee added a commit to workingjubilee/rustc that referenced this issue Mar 7, 2024
Improve std::fs::read_to_string example

Resolves  [rust-lang#118621](rust-lang#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

```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 8, 2024
Improve std::fs::read_to_string example

Resolves  [rust-lang#118621](rust-lang#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

```
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 8, 2024
Improve std::fs::read_to_string example

Resolves  [rust-lang#118621](rust-lang#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

```
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Mar 8, 2024
Rollup merge of rust-lang#118623 - haydonryan:master, r=workingjubilee

Improve std::fs::read_to_string example

Resolves  [rust-lang#118621](rust-lang#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

```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants