@@ -363,6 +363,84 @@ mod fn_keyword { }
363
363
/// [Reference]: https://doc.rust-lang.org/reference/expressions/loop-expr.html#iterator-loops
364
364
mod for_keyword { }
365
365
366
+ #[ doc( keyword = "if" ) ]
367
+ //
368
+ /// If statements and expressions.
369
+ ///
370
+ /// `if` is a familiar construct to most programmers, and is the main way you'll often do logic in
371
+ /// your code. However, unlike in most languages, `if` blocks can also act as expressions.
372
+ ///
373
+ /// ```rust
374
+ /// # let rude = true;
375
+ /// if 1 == 2 {
376
+ /// println!("whoops, mathematics broke");
377
+ /// } else {
378
+ /// println!("everything's fine!");
379
+ /// }
380
+ ///
381
+ /// let greeting = if rude {
382
+ /// "sup nerd."
383
+ /// } else {
384
+ /// "hello, friend!"
385
+ /// };
386
+ ///
387
+ /// if let Ok(x) = "123".parse::<i32>() {
388
+ /// println!("{} double that and you get {}!", greeting, x * 2);
389
+ /// }
390
+ /// ```
391
+ ///
392
+ /// Shown above are the three typical forms an `if` block comes in. First is the usual kind of
393
+ /// thing you'd see in many languages, with an optional `else` block. Second uses `if` as an
394
+ /// expression, which is only possible if all branches return the same type. An `if` expression can
395
+ /// be used everywhere you'd expect. The third kind of `if` block is an `if let` block, which
396
+ /// behaves similarly to using a [`match`] expression:
397
+ ///
398
+ /// ```rust
399
+ /// if let Some(x) = Some(123) {
400
+ /// // code
401
+ /// # let _ = x;
402
+ /// } else {
403
+ /// // something else
404
+ /// }
405
+ ///
406
+ /// match Some(123) {
407
+ /// Some(x) => {
408
+ /// // code
409
+ /// # let _ = x;
410
+ /// },
411
+ /// _ => {
412
+ /// // something else
413
+ /// },
414
+ /// }
415
+ /// ```
416
+ ///
417
+ /// See [`let`] for more information on pattern bindings.
418
+ ///
419
+ /// Each kind of `if` expression can be mixed and matched as needed.
420
+ ///
421
+ /// ```rust
422
+ /// if true == false {
423
+ /// println!("oh no");
424
+ /// } else if "something" == "other thing" {
425
+ /// println!("oh dear");
426
+ /// } else if let Some(200) = "blarg".parse::<i32>().ok() {
427
+ /// println!("uh oh");
428
+ /// } else {
429
+ /// println!("phew, nothing's broken");
430
+ /// }
431
+ /// ```
432
+ ///
433
+ /// The `if` keyword is used in one other place in Rust, namely as a part of pattern matching
434
+ /// itself, allowing patterns such as `Some(x) if x > 200` to be used.
435
+ ///
436
+ /// For more information on `if` expressions, see the [Rust book] or the [Reference].
437
+ ///
438
+ /// [`match`]: keyword.match.html
439
+ /// [`let`]: keyword.let.html
440
+ /// [Rust book]: https://doc.rust-lang.org/stable/book/2018-edition/ch03-05-control-flow.html#if-expressions
441
+ /// [Reference]: https://doc.rust-lang.org/reference/expressions/if-expr.html
442
+ mod if_keyword { }
443
+
366
444
#[ doc( keyword = "let" ) ]
367
445
//
368
446
/// The `let` keyword.
0 commit comments