Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helpful hint in io docs about how ? is not allowed in main() #40763

Merged
merged 5 commits into from
Apr 1, 2017
Merged

Add helpful hint in io docs about how ? is not allowed in main() #40763

merged 5 commits into from
Apr 1, 2017

Conversation

pirate
Copy link
Contributor

@pirate pirate commented Mar 23, 2017

This is my effort to help alleviate the confusion caused by the error message:

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.

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.

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @sfackler (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@sfackler
Copy link
Member

r? @steveklabnik

Copy link
Member

@steveklabnik steveklabnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some small stylistic tweaks here, but adding this here makes sense to me, thanks :)

/cc @rust-lang/docs

@@ -144,6 +144,16 @@
//! # Ok(())
//! # }
//! ```
//! Note that you cannot use the `?` operator in functions that do not return a `Result` (e.g. `main()`).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a new //! line above this to give it some room to breathe?

also, this should be main, not main(), and Result<T, E> not Result

//!
//! ```
//! let mut input = String::new();
//! match io::stdin().read_line(&mut input) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a //! line above this one?

@pirate
Copy link
Contributor Author

pirate commented Mar 23, 2017

Changes made 👍 thanks.

@pirate
Copy link
Contributor Author

pirate commented Mar 23, 2017

This may not be worth adding now, considering the ? in main issue may soon be fixed: rust-lang/rfcs#1937 & https://internals.rust-lang.org/t/rfc-mentoring-opportunity-permit-in-main/4600. Up to you.

@steveklabnik
Copy link
Member

Still has some tidy issues: https://travis-ci.org/rust-lang/rust/jobs/214345350#L566-L568

I'm still okay with landing it even if the other stuff is going to get better

@pirate
Copy link
Contributor Author

pirate commented Mar 28, 2017

Hmm no trailing whitespace and lines are <= 80 char, not sure why the build is failing still. The log is too long to display in the browser, so I couldn't see any error messages.

@frewsxcv
Copy link
Member

Looks like you'll need to add in the import.

[01:06:36] failures:
[01:06:36] 
[01:06:36] ---- io/mod.rs - io (line 157) stdout ----
[01:06:36] 	error[E0433]: failed to resolve. Use of undeclared type or module `io`
[01:06:36]  --> <anon>:6:1
[01:06:36]   |
[01:06:36] 6 | io::stdin().read_line(&mut input).unwrap();
[01:06:36]   | ^^^^^^^^^ Use of undeclared type or module `io`
[01:06:36] 
[01:06:36] error: aborting due to previous error(s)
[01:06:36] 
[01:06:36] thread 'rustc' panicked at 'Box<Any>', /checkout/src/librustc/session/mod.rs:203
[01:06:36] note: Run with `RUST_BACKTRACE=1` for a backtrace.

@frewsxcv
Copy link
Member

The log is too long to display in the browser, so I couldn't see any error messages.

For the record, you can press the 'Raw log' button in Travis CI to see the full log.

@pirate
Copy link
Contributor Author

pirate commented Mar 28, 2017

Wow, I've never seen a linter run on doc example code before. Didn't realize that all code snippets even in the docs must be complete and independently runnable.

@steveklabnik
Copy link
Member

Yup! You can control this with annotations, see here: https://doc.rust-lang.org/stable/book/documentation.html#documentation-as-tests

@steveklabnik
Copy link
Member

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Mar 31, 2017

📌 Commit cd2ec7e has been approved by steveklabnik

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Mar 31, 2017
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 rust-lang#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.
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Mar 31, 2017
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 rust-lang#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.
bors added a commit that referenced this pull request Mar 31, 2017
Rollup of 6 pull requests

- Successful merges: #40703, #40728, #40763, #40871, #40935, #40947
- Failed merges:
@bors bors merged commit cd2ec7e into rust-lang:master Apr 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants