Skip to content

Commit

Permalink
Rollup merge of rust-lang#67749 - gilescope:keyword-in, r=Dylan-DPC
Browse files Browse the repository at this point in the history
keyword docs for else and inkeyword docs for else and in.

First cut of else and in keyword docs. Comments and suggestions more than welcome.
  • Loading branch information
Centril authored Mar 18, 2020
2 parents f509b26 + a113609 commit ffa36b8
Showing 1 changed file with 56 additions and 5 deletions.
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

0 comments on commit ffa36b8

Please sign in to comment.