Skip to content

Commit a113609

Browse files
gilescopeGiles Cope
authored and
Giles Cope
committed
keyword docs for else and inkeyword docs for else and in.
1 parent 84d8f9d commit a113609

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

src/libstd/keyword_docs.rs

+56-5
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,55 @@ mod crate_keyword {}
234234

235235
#[doc(keyword = "else")]
236236
//
237-
/// What to do when an [`if`] condition does not hold.
237+
/// What expression to evaluate when an [`if`] condition evaluates to [`false`].
238238
///
239-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
239+
/// `else` expressions are optional. When no else expressions are supplied it is assumed to evaluate
240+
/// to the unit type `()`.
241+
///
242+
/// The type that the `else` blocks evaluate to must be compatible with the type that the `if` block
243+
/// evaluates to.
244+
///
245+
/// As can be seen below, `else` must be followed by either: `if`, `if let`, or a block `{}` and it
246+
/// will return the value of that expression.
247+
///
248+
/// ```rust
249+
/// let result = if true == false {
250+
/// "oh no"
251+
/// } else if "something" == "other thing" {
252+
/// "oh dear"
253+
/// } else if let Some(200) = "blarg".parse::<i32>().ok() {
254+
/// "uh oh"
255+
/// } else {
256+
/// println!("Sneaky side effect.");
257+
/// "phew, nothing's broken"
258+
/// };
259+
/// ```
260+
///
261+
/// Here's another example but here we do not try and return an expression:
262+
///
263+
/// ```rust
264+
/// if true == false {
265+
/// println!("oh no");
266+
/// } else if "something" == "other thing" {
267+
/// println!("oh dear");
268+
/// } else if let Some(200) = "blarg".parse::<i32>().ok() {
269+
/// println!("uh oh");
270+
/// } else {
271+
/// println!("phew, nothing's broken");
272+
/// }
273+
/// ```
274+
///
275+
/// The above is _still_ an expression but it will always evaluate to `()`.
240276
///
277+
/// There is possibly no limit to the number of `else` blocks that could follow an `if` expression
278+
/// however if you have several then a [`match`] expression might be preferable.
279+
///
280+
/// Read more about control flow in the [Rust Book].
281+
///
282+
/// [Rust Book]: ../book/ch03-05-control-flow.html#handling-multiple-conditions-with-else-if
283+
/// [`match`]: keyword.match.html
284+
/// [`false`]: keyword.false.html
241285
/// [`if`]: keyword.if.html
242-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
243286
mod else_keyword {}
244287

245288
#[doc(keyword = "enum")]
@@ -637,10 +680,18 @@ mod impl_keyword {}
637680
//
638681
/// Iterate over a series of values with [`for`].
639682
///
640-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
683+
/// The expression immediately following `in` must implement the [`Iterator`] trait.
641684
///
685+
/// ## Literal Examples:
686+
///
687+
/// * `for _ **in** 1..3 {}` - Iterate over an exclusive range up to but excluding 3.
688+
/// * `for _ **in** 1..=3 {}` - Iterate over an inclusive range up to and includeing 3.
689+
///
690+
/// (Read more about [range patterns])
691+
///
692+
/// [`Iterator`]: ../book/ch13-04-performance.html
693+
/// [`range patterns`]: ../reference/patterns.html?highlight=range#range-patterns
642694
/// [`for`]: keyword.for.html
643-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
644695
mod in_keyword {}
645696

646697
#[doc(keyword = "let")]

0 commit comments

Comments
 (0)