转载

Rust Patterns

if let

if let allows you to combine if and let together to reduce the overhead of certain kinds of pattern matches.

let option = Some(12); if let Some(x) = option {     foo(x); } else {     bar(); }

while let

In a similar fashion, while let can be used when you want to conditionally loop as long as a value matches a certain pattern.

let mut v = vec![1, 3, 5, 7, 11]; while let Some(x) = v.pop() {     println!("{}", x); } 

复合模式

使用 | 来匹配复合模式:

let x = 1;  match x {     1 | 2 => println!("one or two"),     3 => println!("three"),     _ => println!("anything"), } //打印结果: one or two 

解构

如果有一个复杂的数据类型,例如: struct,我们可以使用pattern来解构:

struct Point {     x: i32,     y: i32, }  let origin = Point { x: 0, y: 0 };  match origin {     Point { x, y } => println!("({},{})", x, y), } 

我们使用 : 来指定不同的名字:

struct Point {     x: i32,     y: i32, }  let origin = Point { x: 0, y: 0 };  match origin {     Point { x: x1, y: y1 } => println!("({},{})", x1, y1), }

如果我们只关系其中的某些值,我们不必指定所有的名字:

struct Point {     x: i32,     y: i32, }  let origin = Point { x: 0, y: 0 };  match origin {     Point { x, .. } => println!("x is {}", x), } 

打印出 x is 0

解构 也完全适用于 tupleenums

忽略绑定

match some_value {     Ok(value) => println!("got a value: {}", value),     Err(_) => println!("an error occurred"), }  fn coordinate() -> (i32, i32, i32) {     // generate and return some sort of triple tuple }  let (x, _, z) = coordinate(); 

相似的,我们可以使用 .. 来忽略多个值:

enum OptionalTuple {     Value(i32, i32, i32),     Missing, }  let x = OptionalTuple::Value(5, -2, 3);  match x {     OptionalTuple::Value(..) => println!("Got a tuple!"),     OptionalTuple::Missing => println!("No such luck."), } 

ref 和ref mut

如果需要获取一个引用,我们可以使用 ref 关键字:

let x = 5;  match x {     ref r => println!("Got a reference to {}", r), } 

这里, rmatch 中的数据类型为 &i32 ,换句话说, ref 在使用patterns中创建了一个引用。如果需要一个可变引用,可以使用 ref mut

let mut x = 5;  match x {     ref mut mr => println!("Got a mutable reference to {}", mr), }     

Ranges

我们使用 ... 来匹配一个范围的值:

let x = 1;  match x {     1 ... 5 => println!("one through five"),     _ => println!("anything"), } 

绑定

我们可以通过 @ 绑定值到一个命名变量上:

let x = 1;  match x {     e @ 1 ... 5 => println!("got a range element {}", e),     _ => println!("anything"), } 

在匹配复杂的数据结构中是非常有用的,例如:

#[derive(Debug)] struct Person {     name: Option<String>, }  let name = "Steve".to_string(); let mut x: Option<Person> = Some(Person { name: Some(name) }); match x {     Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),     _ => {} } 

使用 @| ,可以分别匹配不同的部分:

let x = 5;  match x {     e @ 1 ... 5 | e @ 8 ... 10 => println!("got a range element {}", e),     _ => println!("anything"), } 

关卡

enum OptionalInt {     Value(i32),     Missing, }  let x = OptionalInt::Value(5);  match x {     OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),     OptionalInt::Value(..) => println!("Got an int!"),     OptionalInt::Missing => println!("No such luck."), } 
原文  https://segmentfault.com/a/1190000004912129
正文到此结束
Loading...