A blank identifier uses an underscore to accept or match a value without giving you a usable variable. In match statements, _ means “anything else,” helping a match cover every possibility. section 3.2.2
Catch every remaining match
A match on a u8 must account for values beyond 0, 1, and 2. Add _ for the remaining values: match my_number { 0 => ..., 1 => ..., 2 => ..., _ => println!("It's some other number") }. The wildcard arm runs when no earlier arm matches.
Use the blank identifier when a value is required by syntax but its name or contents are unnecessary:
- Use _ in a for loop when each iteration should run but the iteration value is irrelevant: for _ in 0..3 { ... }. section 3.2.3
- Use |_| in a closure when the closure must accept one argument but does not use it. Writing || instead produces an argument-count error. section 8.3.3
- Remember that match arms are checked from top to bottom. A wildcard can make code compile while an earlier arm prevents later patterns from being reached. section 3.2.2
Ignore a closure argument
For an iterator whose callback receives one item, write .for_each(|_| println!("We didn't use the variables at all")). The closure accepts the required argument and deliberately ignores it.
