18

I want to compare a value to constants, or other values. In another language (e.g JavaScript) I would do:

// Pretend these are values that are generated another way
let a = 23;
let b = 16;
let c = 43;
let input = 23;

switch (input) {
    case a: console.log("Input is equal to a"); break;
    case b: console.log("Input is equal to b"); break;
    case c: console.log("Input is equal to c"); break;
    default: console.log("Input does not equal any value"); break;
}

How would I do this neatly in Rust? I know I could do it with if statements, but I think this is messy and I am comparing to many more values.

Can I compare a variable to constant values in Rust using a match statement?

6
  • 1
    Just to clarify, are a b and c constants or generated at runtime?
    – SCappella
    Commented Apr 23, 2019 at 5:14
  • @User Yes, they are.
    – flmng0
    Commented Apr 23, 2019 at 5:28
  • 1
    If they're generated at runtime, they aren't constants (in Rust anyway). Which one is it?
    – SCappella
    Commented Apr 23, 2019 at 5:29
  • 2
    I recommend you to read the book to learn Rust. It seems that you haven't read it yet. Especially look at chapter 6 for an answer.
    – hellow
    Commented Apr 23, 2019 at 5:36
  • 1
    Possible duplicate of Is it possible to use statements in pattern matching branch? Commented Apr 23, 2019 at 15:18

2 Answers 2

31

The simplest way to do this sort of case analysis is when you know the values of the cases beforehand and don't mind having them in the middle of the code. In this case, a simple match expression is what you want.

fn main() {
    for &input in &[16, 23, 42, 43] {
        match input {
            23 => println!("Input is equal to a"),
            16 => println!("Input is equal to b"),
            43 => println!("Input is equal to c"),
            _ => println!("Input does not equal any value"),
        }
    }
}

(playground link)

If your a b and c are compile-time constants (either a known value or generated with const functions), then you can still directly match on them.

const A: i32 = 23;
const B: i32 = 16;
const C: i32 = generate_c();

const fn generate_c() -> i32 {
    A + B + 4
}

fn main() {
    for &input in &[16, 23, 42, 43] {
        match input {
            A => println!("Input is equal to a"),
            B => println!("Input is equal to b"),
            C => println!("Input is equal to c"),
            _ => println!("Input does not equal any value"),
        }
    }
}

(playground link)

However, if you try this with non-constant variables, you'll get weird outputs.

fn generate_c(a: i32, b: i32) -> i32 {
    a + b + 4
}

fn main() {
    let a = 23;
    let b = 16;
    let c = generate_c(a, b);

    for &input in &[16, 23, 42, 43] {
        match input {
            a => println!("Input is equal to a"),
            b => println!("Input is equal to b"),
            c => println!("Input is equal to c"),
            _ => println!("Input does not equal any value"),
        }
    }
}

(playground link)

If you run this, the compiler will give you lots of warnings about "unreachable patterns" and the output will be "Input is equal to a" all four times. The problem with this is that the left side of each line in a match statement is not simply an expression, but rather a pattern.

A pattern is an expression like (x, [_, z], Some(_)). It's built up from basic variables (like x and z), underscores (_), all the literal expressions (integers, floats, tuples, arrays) and a few other things.

When Rust runs a match statement like this, it tries to syntactically match the input with the pattern. Basic variables will match anything and the value of that variable is set to whatever it matched for the scope of that branch of the match statement. Underscores (as used in all of the above examples) match anything too, but don't bind any variables.

With the const version above, the constants A B and C are replaced with their respective literal values everywhere in the code, so the input is matched on those literal values.

With the variable version, when we match with a b and c, these letters are interpreted as basic variables that match anything. The values of the variables aren't considered at all in the pattern. In the code

let a = 14;
let b = 15;
let c = 16;
let input = 16;
match input {
    a => println!("Input is equal to a"),
    b => println!("Input is equal to b"),
    c => println!("Input is equal to c"),
    _ => println!("Input does not equal any value"),
}

the first branch will always match, giving the input the name a for the scope of the branch.

If you need to match on variables a b and c, you could add a guard to each branch. A guard filters the branch a little more by adding an additional condition for a branch to match. In this case, we match anything and bind it to the variable x, but then check that x is equal to a (and b and c).

fn generate_c(a: i32, b: i32) -> i32 {
    a + b + 4
}

fn main() {
    let a = 23;
    let b = 16;
    let c = generate_c(a, b);

    for &input in &[16, 23, 42, 43] {
        match input {
            x if x == a => println!("Input is equal to a"),
            x if x == b => println!("Input is equal to b"),
            x if x == c => println!("Input is equal to c"),
            _ => println!("Input does not equal any value"),
        }
    }
}

(playground link)

This is a little bit more verbose than the switch/case construction, but I hope it's clear what's going on. At each branch, the variable x is bound to 16, then if that's equal to the variable A (or B or C), then that branch is taken. Otherwise, we try the next branch.

2
  • 2
    You could also mention, that you can use literals in your patterns, e.g. match input { 8 => {}, 16 => {}, 30 => {} }
    – hellow
    Commented Apr 23, 2019 at 5:59
  • If you know for certain other cases can't be reached, you can use _ => unreachable!() Commented Dec 2, 2021 at 7:19
2

Can I compare a variable to constant values in Rust using a match statement?

I wrote a macro to (almost) do this. It expands to a match with if guards.

switch! { input;
    a => println!("Input is equal to a"),
    b => println!("Input is equal to b"),
    c => println!("Input is equal to c"),
    _ => println!("Input does not equal any value"),
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.