@@ -514,32 +514,66 @@ mod impl_keyword { }
514
514
515
515
#[ doc( keyword = "let" ) ]
516
516
//
517
- /// The `let` keyword.
517
+ /// The variable binding keyword.
518
518
///
519
- /// The `let` keyword is used to declare a variable.
520
- ///
521
- /// Example:
519
+ /// The primary use for the `let` keyword is in `let` statements, which are used to introduce a new
520
+ /// set of variables into the current scope, as given by a pattern.
522
521
///
523
522
/// ```rust
524
523
/// # #![allow(unused_assignments)]
525
- /// let x = 3; // We create a variable named `x` with the value `3`.
524
+ /// let thing1: i32 = 100;
525
+ /// let thing2 = 200 + thing1;
526
+ ///
527
+ /// let mut changing_thing = true;
528
+ /// changing_thing = false;
529
+ ///
530
+ /// let (part1, part2) = ("first", "second");
531
+ ///
532
+ /// struct Example {
533
+ /// a: bool,
534
+ /// b: u64,
535
+ /// }
536
+ ///
537
+ /// let Example { a, b: _ } = Example {
538
+ /// a: true,
539
+ /// b: 10004,
540
+ /// };
541
+ /// assert!(a);
526
542
/// ```
527
543
///
528
- /// By default, all variables are **not** mutable. If you want a mutable variable,
529
- /// you'll have to use the `mut` keyword.
544
+ /// The pattern is most commonly a single variable, which means no pattern matching is done and
545
+ /// the expression given is bound to the variable. Apart from that, patterns used in `let` bindings
546
+ /// can be as complicated as needed, given that the pattern is exhaustive. See the [Rust
547
+ /// book][book1] for more information on pattern matching. The type of the pattern is optionally
548
+ /// given afterwards, but if left blank is automatically inferred by the compiler if possible.
530
549
///
531
- /// Example:
550
+ /// Variables in Rust are immutable by default, and require the [`mut`] keyword to be made mutable.
532
551
///
533
- /// ```rust
534
- /// # #![allow(unused_assignments)]
535
- /// let mut x = 3; // We create a mutable variable named `x` with the value `3`.
552
+ /// Multiple variables can be defined with the same name, known as shadowing. This doesn't affect
553
+ /// the original variable in any way beyond being unable to directly access it beyond the point of
554
+ /// shadowing. It continues to remain in scope, getting dropped only when it falls out of scope.
555
+ /// Shadowed variables don't need to have the same type as the variables shadowing them.
536
556
///
537
- /// x += 4; // `x` is now equal to `7`.
557
+ /// ```rust
558
+ /// let shadowing_example = true;
559
+ /// let shadowing_example = 123.4;
560
+ /// let shadowing_example = shadowing_example as u32;
561
+ /// let mut shadowing_example = format!("cool! {}", shadowing_example);
562
+ /// shadowing_example += " something else!"; // not shadowing
538
563
/// ```
539
564
///
540
- /// For more information about the `let` keyword, take a look at the [Rust Book][book].
565
+ /// Other places the `let` keyword is used include along with [`if`], in the form of `if let`
566
+ /// expressions. They're useful if the pattern being matched isn't exhaustive, such as with
567
+ /// enumerations.
568
+ ///
569
+ /// For more information on the `let` keyword, see the [Rust book] or the [Reference]
541
570
///
542
- /// [book]: https://doc.rust-lang.org/book/second-edition/ch03-01-variables-and-mutability.html
571
+ /// [book1]: https://doc.rust-lang.org/stable/book/2018-edition/ch06-02-match.html
572
+ /// [`mut`]: keyword.mut.html
573
+ /// [`if`]: keyword.if.html
574
+ /// [book2]:
575
+ /// https://doc.rust-lang.org/stable/book/2018-edition/ch18-01-all-the-places-for-patterns.html#let-statements
576
+ /// [Reference]: https://doc.rust-lang.org/reference/statements.html#let-statements
543
577
mod let_keyword { }
544
578
545
579
#[ doc( keyword = "struct" ) ]
0 commit comments