Skip to content
Tutorials Center
My learning

Topics

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).

Rust
let kind = if n < 0 { "negative" } else if n == 0 { "zero" } else { "positive" };
if let Some(x) = maybe { println!("{x}"); }

Try it

Rust if as an expression

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.

Rust if let and let chains

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

Rust
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 { } or if name { } does not compile; write count != 0 or !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 of value.
  • Early return reads well with guard clauses: if bad { return Err(...); }, but for failing patterns prefer let ... else.
  • if let versus match: use if let when one arm matters; switch to match when you need exhaustiveness or several arms.
  • Let chains need edition 2024 and Rust 1.88 or newer. In older editions, nest the if let blocks or use matches!.
  • Temporary scope (edition 2024): temporaries created in the scrutinee of if let are dropped before the else branch runs, which fixes some surprising lock-guard deadlocks of earlier editions.
  • Short-circuiting: && and || stop early, so if i < v.len() && v[i] > 0 is safe.
  • else if let is allowed and often replaces a small match.