How to use instanced struct values in a match statement?

Hello, everyone! Just beginning to learn Rust and I’ve successfully run into my first roadblock (correct use of successfully? oh well…)

I have an example instanced struct, named args, of the original struct Args.

struct Args {
    arg1: bool,
    arg2: bool,
}

and the instanced struct being:

let args = Args {
    arg1: true,
    arg2: false,
}

Using these arguments, I am attempting to avoid a jumble of if-else statments and just use a match statement. However, when attempting to perform the following:

match true {
    args.arg1 => println!("Argument 1 is true!"),
    args.arg2 => println!("Argument 2 is true!"),
}

I am given the error

error: expected one of `=>`, `@`, `if`, or `|`, found `.`
  --> src/main.rs:13:13
   |
13 |         args.arg1 => println!("Argument one is true"),
   |             ^ expected one of `=>`, `@`, `if`, or `|` here

error: aborting due to previous error

Is there an escape character sequence I should use to avoid this, or is this simply incorrect syntax?

p.s. I threw this originally on stackoverflow, but, seeing as I am new to both places I figured I might as well go with the community dedicated to Rust itself… :slightly_smiling_face:

this is incorrect synatax args.arg1 is not a valid pattern.

a valid pattern would be something like this

    match args {
        Args {arg1: true, arg2: false} => println!("Argument 1 is true!"),
        Args {arg1: false, arg2: true} => println!("Argument 2 is true!"),
        _ => println!("Both or nothing is true"),
    };

You can’t use variables in the match arms. It’s a match of a value against constants.

2 Likes