liveBook · Manning

Blank identifier

As explained in Learn Rust in a Month of Lunches

A blank identifier is a language-defined name or symbol that receives or matches a value without binding it to a usable variable.

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.

How Learn Rust in a Month of Lunches covers it

How deep it goes
Explains wildcard matching and ignored parameters through small examples, stopping short of broader pattern-binding rules.
Examples
Demonstrates fallback number matching, repeated output, color tuples, and an iterator closure that ignores its argument.
What it assumes
Assumes basic familiarity with match statements, loops, closures, and compiler messages while explaining underscore uses directly.
What you can do after
Apply underscores to satisfy match coverage and required loop or closure parameters without retaining unused values.
How it connects
Connects blank identifiers with match statements, exhaustive patterns, loops, closures, iterators, and compiler diagnostics.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.