diff options
| author | Alexis Bourget <alexis.bourget@gmail.com> | 2020-06-24 09:23:55 +0200 |
|---|---|---|
| committer | Alexis Bourget <alexis.bourget@gmail.com> | 2020-06-24 09:23:55 +0200 |
| commit | d8ea10c95fb5cf1674666fe4eae10ed761e2bfaa (patch) | |
| tree | 310b28bd9213ee9f4abbd8a84539621ac48c5f46 | |
| parent | dcd470fe1be03136a8e1794b7e2cc6179bbd9d92 (diff) | |
| download | rust-d8ea10c95fb5cf1674666fe4eae10ed761e2bfaa.tar.gz rust-d8ea10c95fb5cf1674666fe4eae10ed761e2bfaa.zip | |
Document the return keyword
Apply suggestions from code review Co-authored-by: Josh Triplett <josh@joshtriplett.org>
| -rw-r--r-- | src/libstd/keyword_docs.rs | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs index a4996d9eee8..b2658529e57 100644 --- a/src/libstd/keyword_docs.rs +++ b/src/libstd/keyword_docs.rs @@ -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")] |
