Выбрать главу

Note that the compiler does not check arbitrary expressions for whether all possible conditions have been checked. Therefore, you must use the _ pattern at the end.

fn main() {

let number: u8 = 4;

match number {

i if i == 0 => println!("Zero"),

i if i > 0 => println!("Greater than zero"),

_ => println!("Fell through"), // This should not be possible to reach

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Tuples

Indirectly accessing a variable makes it impossible to branch and use that variable without re-binding. match provides the @ sigil for binding values to names:

// A function `age` which returns a `u32`.

fn age() -> u32 {

15

}

fn main() {

println!("Tell me what type of person you are");

match age() {

0 => println!("I haven't celebrated my first birthday yet"),

// Could `match` 1 ..= 12 directly but then what age

// would the child be? Instead, bind to `n` for the

// sequence of 1 ..= 12. Now the age can be reported.

n @ 1 ..= 12 => println!("I'm a child of age {:?}", n),

n @ 13 ..= 19 => println!("I'm a teen of age {:?}", n),

// Nothing bound. Return the result.

n => println!("I'm an old person of age {:?}", n),

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

You can also use binding to "destructure" enum variants, such as Option:

fn some_number() -> Option<u32> {

Some(42)

}

fn main() {

match some_number() {

// Got `Some` variant, match if its value, bound to `n`,

// is equal to 42.

Some(n @ 42) => println!("The Answer: {}!", n),

// Match any other number.

Some(n) => println!("Not interesting... {}", n),

// Match anything else (`None` variant).

_ => (),

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

functions, enums and Option

For some use cases, when matching enums, match is awkward. For example:

#![allow(unused)]

fn main() {

// Make `optional` of type `Option<i32>`

let optional = Some(7);

match optional {

Some(i) => {

println!("This is a really long string and `{:?}`", i);

// ^ Needed 2 indentations just so we could destructure

// `i` from the option.

},

_ => {},

// ^ Required because `match` is exhaustive. Doesn't it seem

// like wasted space?

};

}

if let is cleaner for this use case and in addition allows various failure options to be specified:

fn main() {

// All have type `Option<i32>`

let number = Some(7);

let letter: Option<i32> = None;

let emoticon: Option<i32> = None;

// The `if let` construct reads: "if `let` destructures `number` into

// `Some(i)`, evaluate the block (`{}`).

if let Some(i) = number {

println!("Matched {:?}!", i);

}

// If you need to specify a failure, use an else:

if let Some(i) = letter {

println!("Matched {:?}!", i);

} else {

// Destructure failed. Change to the failure case.

println!("Didn't match a number. Let's go with a letter!");

}

// Provide an altered failing condition.

let i_like_letters = false;

if let Some(i) = emoticon {

println!("Matched {:?}!", i);

// Destructure failed. Evaluate an `else if` condition to see if the