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

[docs] Improve try! docs to make clearer it returns Result. #25990

Merged
merged 5 commits into from
Jun 4, 2015
Merged

[docs] Improve try! docs to make clearer it returns Result. #25990

merged 5 commits into from
Jun 4, 2015

Conversation

carols10cents
Copy link
Member

The API documentation is not explicit enough that because try! returns
Err early for you, you can only use it in functions that return
Result. The book mentions this, but if you come across try! outside
of the book and look it up in the docs, this restriction on the return
type of the function is not particularly clear.

I seriously had this epiphany a few days ago after working with Rust for MONTHS, and after seeing a friend have to come to the same realization, I'd like to save more people from this confusion :) 💖

The API documentation is not explicit enough that because `try!` returns
`Err` early for you, you can only use it in functions that return
`Result`. The book mentions this, but if you come across `try!` outside
of the book and look it up in the docs, this restriction on the return
type of the function is not particularly clear.
@rust-highfive
Copy link
Collaborator

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

/// let mut file = try!(File::create("my_best_friends.txt"));
/// match file.write_line("This is a list of my best friends.") {
/// Ok(_) => (),
/// Err(e) => return Err(e),
Copy link
Member

Choose a reason for hiding this comment

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

Could this be sure to use 4-space tabs instead of 2-space tabs?

Also, I think the write_line function has since been removed (old I/O?), but perhaps write_all with a byte-string literal could be used? I think this example may also need to import std::io::prelude::* to get the Write trait.

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely!

@joelmccracken
Copy link

This note would have saved me a bunch of time. 👍

@carols10cents
Copy link
Member Author

Thank you @alexcrichton, I made a bunch of tiny commits but I will squash them down into the first one when you've had a chance to take a look. I swear this now passes make check-stage1-doc-crate-std and make check-stage2-doc-crate-std and I won't submit anything without running the tests ever again :) 💟

@killercup
Copy link
Member

Very nice!

I think I'd add another notion that this explicitly cannot be used in fn main -- just to give Google a match for "rust try! main" ;)

@nikomatsakis
Copy link
Contributor

This looks good to me :)

@nikomatsakis
Copy link
Contributor

@bors rollup r+

@bors
Copy link
Contributor

bors commented Jun 3, 2015

📌 Commit c692d75 has been approved by nikomatsakis

@bluss
Copy link
Member

bluss commented Jun 3, 2015

@steveklabnik Can I suggest this for beta backport?

@steveklabnik
Copy link
Member

steveklabnik commented Jun 3, 2015 via email

@bluss
Copy link
Member

bluss commented Jun 3, 2015

Ok. I thought docs would be a risk free backport, so it would be easy & beneficial to backport. Some other projects freeze code first and then allow extra time to only work on bugs & docs.

@steveklabnik
Copy link
Member

We're being very, very conservative at first.

Manishearth added a commit to Manishearth/rust that referenced this pull request Jun 4, 2015
The API documentation is not explicit enough that because `try!` returns
`Err` early for you, you can only use it in functions that return
`Result`. The book mentions this, but if you come across `try!` outside
of the book and look it up in the docs, this restriction on the return
type of the function is not particularly clear.

I seriously had this epiphany a few days ago after working with Rust for MONTHS, and after seeing [a friend have to come to the same realization](http://joelmccracken.github.io/entries/a-simple-web-app-in-rust-pt-2a/), I'd like to save more people from this confusion :) 💖
bors added a commit that referenced this pull request Jun 4, 2015
@bors bors merged commit c692d75 into rust-lang:master Jun 4, 2015
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.

9 participants