-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[docs] Improve try!
docs to make clearer it returns Result
.
#25990
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
Changes from 1 commit
9634bcb
d328d64
a41fd59
80322e2
c692d75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,7 +117,33 @@ macro_rules! println { | |
} | ||
|
||
/// Helper macro for unwrapping `Result` values while returning early with an | ||
/// error if the value of the expression is `Err`. | ||
/// error if the value of the expression is `Err`. Can only be used in | ||
/// functions that return `Result` because of the early return of `Err` that | ||
/// it provides. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use std::io; | ||
/// use std::fs::File; | ||
/// | ||
/// fn write_to_file_using_try() -> Result<(), io::Error> { | ||
/// let mut file = try!(File::create("my_best_friends.txt")); | ||
/// try!(file.write_line("This is a list of my best friends.")); | ||
/// println!("I wrote to the file"); | ||
/// Ok() | ||
/// } | ||
/// // This is equivalent to: | ||
/// fn write_to_file_using_match() -> Result<(), io::Error> { | ||
/// 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), | ||
/// } | ||
/// println!("I wrote to the file"); | ||
/// Ok() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may want to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, I submitted this as I was rebuilding/running the doc tests and they failed. Sorry for being overconfident and impatient :) |
||
/// } | ||
/// ``` | ||
#[macro_export] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
macro_rules! try { | ||
|
There was a problem hiding this comment.
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 perhapswrite_all
with a byte-string literal could be used? I think this example may also need to importstd::io::prelude::*
to get theWrite
trait.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely!