Rust Reference ยท Keywords and Statements
The if and else expressions
Reference for Rust if and else: conditions as expressions, else if chains, if let, let chains in edition 2024, type rules for branches and common mistakes.
if runs a block when a condition is true, and else provides the alternative. In Rust if is an expression: it has a value, so you can write let size = if big { 10 } else { 1 };. The condition must be a bool; Rust never converts numbers, strings or options to truthiness, and no parentheses are needed around it.
if let tests a pattern instead of a boolean, which makes it the compact way to handle one interesting case of an enum such as Option. In edition 2024, since Rust 1.88, you can combine several let patterns and boolean tests with && in one condition (a let chain).
let kind = if n < 0 { "negative" } else if n == 0 { "zero" } else { "positive" }; if let Some(x) = maybe { println!("{x}"); }
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
if condition { ... } if condition { ... } else { ... } if condition { ... } else if other { ... } else { ... } if let PATTERN = expression { ... } else { ... } if let PATTERN = expr && condition && let OTHER = expr2 { ... }
| Part | Description |
|---|---|
condition |
An expression of type bool. A struct literal is not allowed directly in it without parentheses. |
PATTERN = expression |
Matches when the pattern fits; bindings are visible only inside the block that follows. |
| Branch blocks | Each branch is a block whose last expression (without a semicolon) is its value. |
Type rule: all branches of an if that is used as a value must have the same type, and an if without else can only have type (). Violations are E0308 or E0317.
Usage notes
- No truthiness.
if count { }orif name { }does not compile; writecount != 0or!name.is_empty(). - No ternary operator. Use
if cond { a } else { b }, which fits on one line. - Mind the semicolon.
if x { 1 } else { 2 };discards the value, and{ value; }makes the block return()instead ofvalue. - Early return reads well with guard clauses:
if bad { return Err(...); }, but for failing patterns preferlet ... else. if letversusmatch: useif letwhen one arm matters; switch tomatchwhen you need exhaustiveness or several arms.- Let chains need edition 2024 and Rust 1.88 or newer. In older editions, nest the
if letblocks or usematches!. - Temporary scope (edition 2024): temporaries created in the scrutinee of
if letare dropped before theelsebranch runs, which fixes some surprising lock-guard deadlocks of earlier editions. - Short-circuiting:
&&and||stop early, soif i < v.len() && v[i] > 0is safe. else if letis allowed and often replaces a smallmatch.