if then match rust code example

Example 1: if in rust

if <boolean expression> {
		print!("result from expression is true");
	}
    else {
        print!("result from expression is false");
	}

Example 2: 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);
}

Tags:

Rust Example