From 4dc122580714a5f8859e993bf56a7228b0bcd7c1 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Thu, 23 Mar 2017 13:17:21 -0400 Subject: [PATCH 1/5] Add helpful hint on io function for beginners --- src/libstd/io/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 850885a8c0f3a..dda9d6bca7977 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -144,6 +144,16 @@ //! # Ok(()) //! # } //! ``` +//! Note that you cannot use the `?` operator in functions that do not return a `Result` (e.g. `main()`). +//! Instead, you can `match` on the return value to catch any possible errors: +//! +//! ``` +//! let mut input = String::new(); +//! match io::stdin().read_line(&mut input) { +//! Err(why) => panic!("Failed to read input: {}", why.description()), +//! Ok(_) => println!("You typed: {}", input.trim()), +//! } +//! ``` //! //! And a very common source of output is standard output: //! From 53d5082a2d08060ebca869cb8ee97c3ed3cf4ce9 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Thu, 23 Mar 2017 13:42:39 -0400 Subject: [PATCH 2/5] requested changes --- src/libstd/io/mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index dda9d6bca7977..60b211a746ff1 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -144,7 +144,8 @@ //! # Ok(()) //! # } //! ``` -//! Note that you cannot use the `?` operator in functions that do not return a `Result` (e.g. `main()`). +//! +//! Note that you cannot use the `?` operator in functions that do not return a `Result` (e.g. `main`). //! Instead, you can `match` on the return value to catch any possible errors: //! //! ``` From 04fbec1a0cb7467834bd264b80350b1cade8b4ca Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Thu, 23 Mar 2017 13:43:09 -0400 Subject: [PATCH 3/5] newline for breathing room --- src/libstd/io/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 60b211a746ff1..773b0964b4269 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -150,6 +150,7 @@ //! //! ``` //! let mut input = String::new(); +//! //! match io::stdin().read_line(&mut input) { //! Err(why) => panic!("Failed to read input: {}", why.description()), //! Ok(_) => println!("You typed: {}", input.trim()), From 4806f01d7c1f35a1b6f675ff099b86ec6e6c1540 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Mon, 27 Mar 2017 16:34:13 -0400 Subject: [PATCH 4/5] Fix tidy errors and simplify example --- src/libstd/io/mod.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 773b0964b4269..1b0c992ba0976 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -145,16 +145,14 @@ //! # } //! ``` //! -//! Note that you cannot use the `?` operator in functions that do not return a `Result` (e.g. `main`). -//! Instead, you can `match` on the return value to catch any possible errors: -//! +//! Note that you cannot use the `?` operator in functions that do not return +//! a `Result` (e.g. `main`). Instead, you can call `.unwrap()` or `match` +//! on the return value to catch any possible errors: +//! //! ``` //! let mut input = String::new(); -//! -//! match io::stdin().read_line(&mut input) { -//! Err(why) => panic!("Failed to read input: {}", why.description()), -//! Ok(_) => println!("You typed: {}", input.trim()), -//! } +//! +//! io::stdin().read_line(&mut input).unwrap(); //! ``` //! //! And a very common source of output is standard output: From cd2ec7eded8d65b88b4c2fdb26efe1b3c505bd6f Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Tue, 28 Mar 2017 13:27:46 -0400 Subject: [PATCH 5/5] add missing import --- src/libstd/io/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 1b0c992ba0976..32ead78f6cd58 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -150,6 +150,8 @@ //! on the return value to catch any possible errors: //! //! ``` +//! use std::io; +//! //! let mut input = String::new(); //! //! io::stdin().read_line(&mut input).unwrap();