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

keyword docs for else and inkeyword docs for else and in. #67749

Merged
merged 1 commit into from
Mar 19, 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
61 changes: 56 additions & 5 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,55 @@ mod crate_keyword {}

#[doc(keyword = "else")]
//
/// What to do when an [`if`] condition does not hold.
/// What expression to evaluate when an [`if`] condition evaluates to [`false`].
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// `else` expressions are optional. When no else expressions are supplied it is assumed to evaluate
/// to the unit type `()`.
///
/// The type that the `else` blocks evaluate to must be compatible with the type that the `if` block
/// evaluates to.
///
/// As can be seen below, `else` must be followed by either: `if`, `if let`, or a block `{}` and it
/// will return the value of that expression.
///
/// ```rust
/// let result = if true == false {
/// "oh no"
/// } else if "something" == "other thing" {
/// "oh dear"
/// } else if let Some(200) = "blarg".parse::<i32>().ok() {
/// "uh oh"
/// } else {
/// println!("Sneaky side effect.");
/// "phew, nothing's broken"
/// };
/// ```
///
/// Here's another example but here we do not try and return an expression:
///
/// ```rust
/// if true == false {
/// println!("oh no");
/// } else if "something" == "other thing" {
/// println!("oh dear");
/// } else if let Some(200) = "blarg".parse::<i32>().ok() {
/// println!("uh oh");
/// } else {
/// println!("phew, nothing's broken");
/// }
/// ```
///
/// The above is _still_ an expression but it will always evaluate to `()`.
///
/// There is possibly no limit to the number of `else` blocks that could follow an `if` expression
/// however if you have several then a [`match`] expression might be preferable.
///
/// Read more about control flow in the [Rust Book].
///
/// [Rust Book]: ../book/ch03-05-control-flow.html#handling-multiple-conditions-with-else-if
/// [`match`]: keyword.match.html
/// [`false`]: keyword.false.html
/// [`if`]: keyword.if.html
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
mod else_keyword {}

#[doc(keyword = "enum")]
Expand Down Expand Up @@ -637,10 +680,18 @@ mod impl_keyword {}
//
/// Iterate over a series of values with [`for`].
///
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
/// The expression immediately following `in` must implement the [`Iterator`] trait.
///
/// ## Literal Examples:
///
/// * `for _ **in** 1..3 {}` - Iterate over an exclusive range up to but excluding 3.
/// * `for _ **in** 1..=3 {}` - Iterate over an inclusive range up to and includeing 3.
///
/// (Read more about [range patterns])
///
/// [`Iterator`]: ../book/ch13-04-performance.html
/// [`range patterns`]: ../reference/patterns.html?highlight=range#range-patterns
/// [`for`]: keyword.for.html
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
mod in_keyword {}

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