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

Document the return keyword #73648

Merged
merged 1 commit into from
Jun 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,9 +1000,55 @@ mod ref_keyword {}
//
/// Return a value from a function.
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// A `return` marks the end of an execution path in a function:
///
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
/// ```
/// fn foo() -> i32 {
/// return 3;
/// }
/// assert_eq!(foo(), 3);
/// ```
///
/// `return` is not needed when the returned value is the last expression in the
/// function. In this case the `;` is omitted:
///
/// ```
/// fn foo() -> i32 {
/// 3
/// }
/// assert_eq!(foo(), 3);
/// ```
///
/// `return` returns from the function immediately (an "early return"):
///
/// ```no_run
/// use std::fs::File;
/// use std::io::{Error, ErrorKind, Read, Result};
///
/// fn main() -> Result<()> {
/// let mut file = match File::open("foo.txt") {
/// Ok(f) => f,
/// Err(e) => return Err(e),
/// };
///
/// let mut contents = String::new();
/// let size = match file.read_to_string(&mut contents) {
/// Ok(s) => s,
/// Err(e) => return Err(e),
/// };
///
/// if contents.contains("impossible!") {
/// return Err(Error::new(ErrorKind::Other, "oh no!"));
/// }
///
/// if size > 9000 {
/// return Err(Error::new(ErrorKind::Other, "over 9000!"));
/// }
///
/// assert_eq!(contents, "Hello, world!");
/// Ok(())
/// }
/// ```
mod return_keyword {}

#[doc(keyword = "self")]
Expand Down