Skip to content

Commit 6edab01

Browse files
authored
Rollup merge of #40763 - pirate:patch-2, r=steveklabnik
Add helpful hint in io docs about how ? is not allowed in main() This is my effort to help alleviate the confusion caused by the error message: ```rust error[E0277]: the trait bound `(): std::ops::Carrier` is not satisfied --> hello_world.rs:72:5 | 72 | io::stdin().read_line(&mut d_input)?; | ------------------------------------ | | | the trait `std::ops::Carrier` is not implemented for `()` | in this macro invocation | = note: required by `std::ops::Carrier::from_error` error: aborting due to previous error ``` This has been discussed at length in #35946, but I figured it would be helpful to mention in the docs. Reading user input is one of the first things beginners will look up in the docs, so my thinking was they'd see this warning here and not have to deal with the [tricky error message](https://blog.rust-lang.org/2017/03/02/lang-ergonomics.html). If you think this isn't the right place to put this in the docs, that's understandable, I'm open to suggestions for putting it elsewhere or removing it entirely.
2 parents c6e2512 + cd2ec7e commit 6edab01

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/libstd/io/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@
145145
//! # }
146146
//! ```
147147
//!
148+
//! Note that you cannot use the `?` operator in functions that do not return
149+
//! a `Result<T, E>` (e.g. `main`). Instead, you can call `.unwrap()` or `match`
150+
//! on the return value to catch any possible errors:
151+
//!
152+
//! ```
153+
//! use std::io;
154+
//!
155+
//! let mut input = String::new();
156+
//!
157+
//! io::stdin().read_line(&mut input).unwrap();
158+
//! ```
159+
//!
148160
//! And a very common source of output is standard output:
149161
//!
150162
//! ```

0 commit comments

Comments
 (0)