'Referring to matched value in Rust

Suppose I have this code:

fn non_zero_rand() -> i32 {
    let x = rand();
    match x {
        0 => 1,
        _ => x,
    }
}

Is there a concise way to put the rand() in the match, and then bind it to a value. E.g. something like this:

fn non_zero_rand() -> i32 {
    match let x = rand() {
        0 => 1,
        _ => x,
    }
}

Or maybe:

fn non_zero_rand() -> i32 {
    match rand() {
        0 => 1,
        _x => _x,
    }
}


Solution 1:[1]

A match arm that consists of just an identifier will match any value, declare a variable named as the identifier, and move the value to the variable. For example:

match rand() {
    0 => 1,
    x => x * 2,
}

A more general way to create a variable and match it is using the @ pattern:

match rand() {
    0 => 1,
    x @ _ => x * 2,
}

In this case it is not necessary, but it can come useful when dealing with conditional matches such as ranges:

match code {
    None => Empty,
    Some(ascii @ 0..=127) => Ascii(ascii as u8),
    Some(latin1 @ 160..=255) => Latin1(latin1 as u8),
    _ => Invalid
}

Solution 2:[2]

You can bind the pattern to a name:

fn non_zero_rand() -> i32 {
    match rand() {
        0 => 1, // 0 is a refutable pattern so it only matches when it fits.
        x => x, // the pattern is x here,
                // which is non refutable, so it matches on everything
                // which wasn't matched already before
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shepmaster