Function std::fs::read_to_string example code fails to run on command line and needs improving. #118621
Closed
Description
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.