Answers for "rust match statement"

2

rust match string

match stringthing.as_str() {
    "a" => println!("0"),
    "b" => println!("1"),
    "c" => println!("2"),
    _ => println!("something else!"),
}
Posted by: Guest on June-13-2020
0

matching in rust

match number {
        // Match a single value
        1 => println!("One!"),
        // Match several values
        2 | 3 | 5 | 7 | 11 => println!("This is a prime"),
        // Match an inclusive range
        13..=19 => println!("A teen"),
        // Handle the rest of cases
        _ => println!("Ain't special"),
    }
Posted by: Guest on July-28-2020
0

rust match statement

#[derive(Debug)] // so we can inspect the state in a minute
enum UsState {
    Alabama,
    Alaska,
    // --snip--
}
#[derive(Debug)]
enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(UsState),
}
fn return_the_coin(coin:Coin) ->u8{
    match coin{
      Coin::Penny =>1,
      Coin::Nickel =>10,
      Coin::Dime =>15,
      Coin::Quarter(state) =>{
          println!("{:#?}",state);
          25
      }
    }
}

fn main() {
    let rtn=return_the_coin(Coin::Quarter(UsState::Alabama));
    println!("{:#?}",rtn);
}
Posted by: Guest on December-23-2020

Browse Popular Code Answers by Language