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:
commit
876847bed8
1 changed files with 4 additions and 4 deletions
|
@ -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(())
|
||||
/// }
|
||||
/// ```
|
||||
|
|
Loading…
Add table
Reference in a new issue