How to Use the Underscore _ Placeholder in Rust

The underscore _ placeholder in Rust ignores values to prevent unused variable warnings.

The underscore _ in Rust is a placeholder that tells the compiler to ignore a value or pattern, preventing unused variable warnings. Use it in function parameters, match arms, or variable bindings when you need to acknowledge a value but not use it.

fn main() {
    let _ignored = 42;
    match 1 {
        1 => println!("one"),
        _ => println!("other"),
    }
    let mut x = 0;
    let _ = x; // Explicitly ignore x to avoid warning
}