Rust Reference ยท Keywords and Statements
The let statement
Reference for Rust let: declaring variable bindings, patterns on the left side, shadowing, deferred initialization, type annotations and let-else.
let introduces a new variable binding in the current block. The thing on the left of the = is not just a name but a pattern, so one let can also take apart tuples, structs and arrays. A binding is immutable unless you add mut, and it lives until the end of its enclosing block, when its value is dropped.
Declaring a name that already exists in scope does not change the old variable; it creates a new one that shadows it. The old value is still alive (and dropped at the end of the block) but can no longer be reached by that name. Since Rust 1.65 you can also write let ... else to bind a refutable pattern and diverge when it does not match.
let name = "Ferris"; let (x, y): (i32, f64) = (3, 1.5); let Some(first) = names.first() else { return; };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
let pattern: Type = expression; let pattern = expression; let name; // declared now, assigned later let pattern = expression else { diverging_block };
| Part | Description |
|---|---|
pattern |
An irrefutable pattern for a plain let (a name, _, a tuple, a struct, an array). A refutable pattern such as Some(x) is only allowed with else. |
: Type |
Optional annotation. Needed when inference cannot decide, for example with parse() or collect(). |
expression |
The initializer. Its value is moved into the pattern (or copied, if the type is Copy). |
else { ... } |
Runs when the pattern does not match. The block must diverge: return, break, continue, panic! or a call to a ! function. |
Errors: using a variable that was declared but not assigned on every path is error E0381. Assigning twice to a non-mut binding is E0384. A let ... else block that can fall through is an error.
Usage notes
- Immutable by default.
let x = 5; x = 6;does not compile. Addmutonly for bindings that really change. - Shadowing is not mutation. It creates a fresh variable, so it can change the type (
let n = n.to_string();) and the old one stays borrowed or alive independently. Amutbinding must keep its type. - Shadowing works well for conversions in a pipeline, which avoids names like
input_strandinput_num. - Underscore patterns:
let _ = expr;drops the value immediately, whilelet _guard = expr;keeps it alive until the end of the block. This difference matters for lock guards and other RAII values. - Deferred initialization lets each branch pick a value without
mut. The compiler checks that exactly one assignment happens on every path. - Temporaries: a
letcan extend the lifetime of a temporary it borrows, solet r = &String::from("x");is fine. - let-else keeps the happy path unindented. Use it instead of a
matchwith a single useful arm, and useif letwhen the failing case needs no early exit. - Unused bindings produce a warning; prefix the name with
_to silence it deliberately. - Edition note: let-else was stabilized in Rust 1.65.
Related
mut,const,static,ifandelse,match- Lessons: Variables and Mutability, Patterns, Syntax