Rust Reference ยท Patterns
Literal and identifier patterns
Reference for Rust literal and identifier patterns: matching exact values, binding names, constants in patterns, and the classic pitfall of a typo becoming a binding.
A pattern describes the shape a value must have. The two simplest patterns are the literal pattern, which matches one exact value such as 0, 'x', true or "yes", and the identifier pattern, which matches anything and binds it to a new name.
Patterns appear in match arms, let statements, function parameters, if let, while let and for loops. A pattern is irrefutable when it always matches (an identifier), and refutable when it can fail (a literal). let and function parameters require irrefutable patterns; match arms and if let accept refutable ones.
match code { 200 => println!("ok"), // literal pattern other => println!("{other}") // identifier pattern, binds the value }
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
42 // integer literal -7 // negative literal (a leading minus is allowed) 'q' // char literal "text" // string literal (only against &str) b'a' // byte literal true // bool literal name // identifier: binds by move or copy mut name // identifier: binds a mutable local ref name // identifier: binds by shared reference ref mut name // identifier: binds by mutable reference CONST_NAME // path to a constant: compared by value
| Pattern | Refutable? | Description |
|---|---|---|
| Literal | Yes | Matches when the value equals the literal. Works for integers, floats (with a lint), char, bool, &str and byte strings. |
| Identifier | No | Matches any value and binds it. Moves non-Copy values out of the matched place. |
| Constant path | Yes | Matches when the value equals the constant. The type must derive PartialEq and Eq. |
Edge cases: a literal must have the same type as the value matched. A string literal pattern only works against &str, not against String; call .as_str() first. Floating-point literal patterns are allowed but discouraged, and a NaN can never match.
Usage notes
- Bindings move by default.
match opt { Some(s) => ..., None => ... }on an ownedOption<String>moves the string intos. Match on&optor userefto keep the original. - Capitalization is a convention that matters.
LIMITresolves to the constant when one is in scope. A lowercase name that is not in scope is a fresh binding, so a mistyped constant silently matches everything. The compiler warns withunreachable patternfor the arms that follow it, which is how you usually notice. - Shadowing, not comparing. An identifier pattern never compares against an existing variable. To compare with a variable's value, use a match guard:
n if n == limit => .... - Exhaustiveness. A
matchmust cover every value. Integer literals alone never do; add an identifier or_arm at the end. mutbindings make a local copy:Some(mut n)lets you changenwithout affecting the originalOptionwhennisCopy.- Enum variants without data, like
None, look like identifiers but resolve to the variant because it is in the prelude. Importing a variant withusemakes bare variant names patterns too.