Читаем Rust by Example полностью

Note that it's possible to manually customize panic with expect, but unwrap otherwise leaves us with a less meaningful output than explicit handling. In the following example, explicit handling yields a more controlled result while retaining the option to panic if desired.

// The commoner has seen it all, and can handle any gift well.

// All gifts are handled explicitly using `match`.

fn give_commoner(gift: Option<&str>) {

// Specify a course of action for each case.

match gift {

Some("snake") => println!("Yuck! I'm putting this snake back in the forest."),

Some(inner) => println!("{}? How nice.", inner),

None => println!("No gift? Oh well."),

}

}

// Our sheltered royal will `panic` at the sight of snakes.

// All gifts are handled implicitly using `unwrap`.

fn give_royal(gift: Option<&str>) {

// `unwrap` returns a `panic` when it receives a `None`.

let inside = gift.unwrap();

if inside == "snake" { panic!("AAAaaaaa!!!!"); }

println!("I love {}s!!!!!", inside);

}

fn main() {

let food = Some("cabbage");

let snake = Some("snake");

let void = None;

give_commoner(food);

give_commoner(snake);

give_commoner(void);

let bird = Some("robin");

let nothing = None;

give_royal(bird);

give_royal(nothing);

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

<p id="unpacking_options_with_"><strong><a l:href="#unpacking_options_with_">Unpacking options with</a><a l:href="#unpacking_options_with_">?</a></strong></p>

You can unpack Options by using match statements, but it's often easier to use the ? operator. If x is an Option, then evaluating x? will return the underlying value if x is Some, otherwise it will terminate whatever function is being executed and return None.

fn next_birthday(current_age: Option) -> Option {

// If `current_age` is `None`, this returns `None`.

// If `current_age` is `Some`, the inner `u8` gets assigned to `next_age`

let next_age: u8 = current_age?;

Some(format!("Next year I will be {}", next_age))

}

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

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Перейти на страницу:

Похожие книги