1
Fork 0

Rollup merge of #67749 - gilescope:keyword-in, r=Dylan-DPC

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.
This commit is contained in:
Mazdak Farrokhzad 2020-03-18 18:03:31 +01:00 committed by GitHub
commit 56106847da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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")]
@ -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")]