Browse Source

The match Operator

master
Saura Sambit 1 year ago
parent
commit
367c5e6e2e
4 changed files with 132 additions and 0 deletions
  1. +78
    -0
      README.md
  2. +6
    -0
      ch06/pattern/Cargo.lock
  3. +9
    -0
      ch06/pattern/Cargo.toml
  4. +39
    -0
      ch06/pattern/src/main.rs

+ 78
- 0
README.md View File

@ -531,3 +531,81 @@ let some_number = Some(5);
let some_string = Some("a string");
let absent_number: Option<i32> = None; //type needs to be specified for None
```
######The `match` Control Flow Operator
Rust has an extremely powerful control flow operator called `match` that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made up of literal values, variable names, wildcards, and many other things.
```
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn main() {
let n = Coin::Penny;
println!("{}", value_in_cents(n));
}
```
######Patterns that Bind to Values
`match` arms can bind to the parts of the values that match the pattern:
```
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
},
}
}
```
######Matching with `Option<T>`
```
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
let five = Some(5);
let six = plus_one(five); //Some(5) => Some(5+1)
let none = plus_one(None); //None => None
```
Matches in Rust are exhaustive, .i.e. we must exhaust every last possibility in order for the code to be valid. Especially in the case of `Option<T>` with the `None` case.
The `_` Placeholder: We can use `_` when we don't want to list all the possible values. For example when we don't want to list all 255 values of `u8` and only care about 1,3,5,7:
```
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (), // matches all possible values that aren't specified before
}
```

+ 6
- 0
ch06/pattern/Cargo.lock View File

@ -0,0 +1,6 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "pattern"
version = "0.1.0"

+ 9
- 0
ch06/pattern/Cargo.toml View File

@ -0,0 +1,9 @@
[package]
name = "pattern"
version = "0.1.0"
authors = ["Saura Sambit <sasach@riseup.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

+ 39
- 0
ch06/pattern/src/main.rs View File

@ -0,0 +1,39 @@
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter(state) => {
println!("State quarter from {:?}!", state);
25
},
}
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
fn main() {
let n = Coin::Quarter(UsState::Alaska);
println!("{}", value_in_cents(n));
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
}

Loading…
Cancel
Save