How to use match statement (switch statement) in Rust

2 Answers

0 votes
fn main() {
    let code = "Z09";
    let state = match code {
      "X03" => {println!("Found match to X03"); "XO3 Code"},
      "Y07" => "Y07 Code",
      "Z09" => {println!("Found match to Z09"); "Z09 Code"},
      "V01" => "V01 Code",
      _ => "Unknown"
   };
   
   println!("State = {}", state);
}




/*
run:

Found match to Z09
State = Z09 Code

*/

 



answered Oct 23, 2022 by avibootz
0 votes
const A: i32 = 546;
const B: i32 = 901;
const C: i32 = set_c();

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

fn main() {
    for &value in &[546, 901, 1447, 5] {
        match value {
            A => println!("value = a"),
            B => println!("value = b"),
            C => println!("value = c"),
            _ => println!("value not equal any number"),
        }
    }
}




/*
run:

value = a
value = b
value = c
value not equal any number

*/

 



answered Oct 23, 2022 by avibootz
edited Oct 23, 2022 by avibootz

Related questions

1 answer 81 views
1 answer 160 views
160 views asked Oct 23, 2022 by avibootz
...