Skip to content

Commit 876847b

Browse files
authored
Rollup merge of #118623 - haydonryan:master, r=workingjubilee
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 ```
2 parents 9fb91aa + b5e1ca3 commit 876847b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

library/std/src/fs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ pub struct DirBuilder {
248248
///
249249
/// ```no_run
250250
/// use std::fs;
251-
/// use std::net::SocketAddr;
252251
///
253252
/// fn main() -> Result<(), Box<dyn std::error::Error + 'static>> {
254-
/// let foo: SocketAddr = String::from_utf8_lossy(&fs::read("address.txt")?).parse()?;
253+
/// let data: Vec<u8> = fs::read("image.jpg")?;
254+
/// assert_eq!(data[0..3], [0xFF, 0xD8, 0xFF]);
255255
/// Ok(())
256256
/// }
257257
/// ```
@@ -290,11 +290,11 @@ pub fn read<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
290290
///
291291
/// ```no_run
292292
/// use std::fs;
293-
/// use std::net::SocketAddr;
294293
/// use std::error::Error;
295294
///
296295
/// fn main() -> Result<(), Box<dyn Error>> {
297-
/// let foo: SocketAddr = fs::read_to_string("address.txt")?.parse()?;
296+
/// let message: String = fs::read_to_string("message.txt")?;
297+
/// println!("{}", message);
298298
/// Ok(())
299299
/// }
300300
/// ```

0 commit comments

Comments
 (0)