Rust Reference ยท Patterns
Wildcard and rest patterns
Reference for Rust's wildcard pattern _ and rest pattern ..: ignoring values, skipping fields, the difference between _ and _name, and ignoring the rest of a tuple or slice.
The wildcard pattern _ matches any single value and throws it away. The rest pattern .. matches zero or more remaining items inside a tuple, a tuple struct, a struct or a slice. Together they let a pattern say "I only care about these parts".
Unlike a named binding, _ does not bind, so it does not move the value. That matters for ownership: let _ = make_string(); still drops the string right away, but match s { _ => {} } leaves s untouched.
let (first, _, _) = (1, 2, 3); // keep only the first let (head, ..) = (1, 2, 3, 4); // same idea with ..
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
_ // any one value .. // the rest, at most once per tuple/slice/struct pattern (a, .., z) // first and last element of a tuple [first, .., last] // first and last element of a slice Struct { f, .. } // struct fields not listed Variant(_, x) // skip positional fields _name // ordinary binding, unused-variable lint silenced
| Form | Description |
|---|---|
_ |
Matches one value of any type and ignores it. Does not move. |
.. |
Matches any number of items (including none). Allowed once in each tuple, tuple struct or slice pattern. In struct patterns it stands for all unlisted fields. |
_name |
A normal identifier binding. The value is moved or copied into it. |
Edge cases: using .. twice in one tuple pattern is a compile error because the split would be ambiguous. A struct pattern without .. must name every field, or the compiler reports a missing-fields error (E0027). In a slice pattern, rest @ .. binds the skipped middle as a subslice (see slice patterns).
Usage notes
_is not a variable. You cannot read it back;println!("{_}")does not compile.- Ownership difference. With
let (_, b) = pair;the first element stays inpairandpair.0is still usable. Withlet (_a, b) = pair;it moves into_a. let _ = value;versuslet _x = value;. The first drops the value at once, the second keeps it alive until the end of the scope. This matters for lock guards and other RAII values:let _ = mutex.lock();releases the lock immediately.- Default arms.
_ => ...is the usual catch-all arm of amatch. Prefer listing variants of your own enums so that adding one forces a compile error rather than silently falling into_. - Closure and function parameters accept
_for arguments you do not need:|_| 0,fn f(_: i32). - Partial matching of enum data.
Message::Move { x, .. }is the idiomatic way to read some fields of a struct-like variant.