How to use match with a enum in Rust

2 Answers

0 votes
#[allow(dead_code)]

enum Temperature {
    Celsius(i32),
    Fahrenheit(i32),
}

fn main() {
    let temperature = Temperature::Celsius(21);
    

    match temperature {
        Temperature::Celsius(t) if t > 20 => println!("{} > 25 Celsius", t),
        Temperature::Celsius(t) => println!("{} < 25 Celsius", t),

        Temperature::Fahrenheit(t) if t > 80 => println!("{} > 80 Fahrenheit", t),
        Temperature::Fahrenheit(t) => println!("{} < 80 Fahrenheit", t),
    }
}




/*
run:

21 > 25 Celsius

*/

 



answered May 5, 2023 by avibootz
0 votes
#[allow(dead_code)]

enum Temperature {
    Celsius(i32),
    Fahrenheit(i32),
}

fn main() {
    let temperature = Temperature::Fahrenheit(70);
    

    match temperature {
        Temperature::Celsius(t) if t > 20 => println!("{} > 25 Celsius", t),
        Temperature::Celsius(t) => println!("{} < 25 Celsius", t),

        Temperature::Fahrenheit(t) if t > 80 => println!("{} > 80 Fahrenheit", t),
        Temperature::Fahrenheit(t) => println!("{} < 80 Fahrenheit", t),
    }
}




/*
run:

70 < 80 Fahrenheit

*/

 



answered May 5, 2023 by avibootz

Related questions

6 answers 491 views
1 answer 208 views
208 views asked May 5, 2023 by avibootz
3 answers 376 views
4 answers 412 views
3 answers 382 views
2 answers 296 views
1 answer 207 views
207 views asked Oct 28, 2022 by avibootz
...