# 模式

let tup = (0u8, 1u8);
let (x, y) = tup;

let x = 1;
let c = 'c';

match c {
x => println!("x: {} c: {}", x, c),
}

println!("x: {}", x);

x: c c: c
x: 1

## 更强大的解构

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

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

struct Point {
x: i64,
y: i64,
}

let point = Point { x: 0, y: 0 };

match point {
Point { y, .. } => println!("y is {}", y),
}

## 忽略和内存管理

let tuple: (u32, String) = (5, String::from("five"));

let (x, s) = tuple;

// 以下行将导致编译错误，因为String类型并未实现Copy, 所以tuple被整体move掉了。
// println!("Tuple is: {:?}", tuple);

let tuple = (5, String::from("five"));

// 忽略String类型，而u32实现了Copy，则tuple不会被move
let (x, _) = tuple;

println!("Tuple is: {:?}", tuple);

## 范围和多重匹配

### 范围

let x = 1;

match x {
1 ... 10 => println!("一到十"),
_ => println!("其它"),
}

let c = 'w';

match c {
'a' ... 'z' => println!("小写字母"),
'A' ... 'Z' => println!("大写字母"),
_ => println!("其他字符"),
}

### 多重匹配

let x = 1;

match x {
1 | 2 => println!("一或二"),
_ => println!("其他"),
}

## ref 和 ref mut

let mut x = 5;

match x {
ref mut mr => println!("mut ref :{}", mr),
}
// 当然了……在let表达式里也能用
let ref mut mrx = x;

## 变量绑定

let x = 1u32;
match x {
e @ 1 ... 5 | e @ 10 ... 15 => println!("get:{}", e),
_ => (),
}

#[derive(Debug)]
struct Person {
name: Option<String>,
}

let name = "Steve".to_string();
let x: Option<Person> = Some(Person { name: Some(name) });
match x {
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
_ => {}
}

Some("Steve")

## 后置条件

let x = 4;
let y = false;

match x {
4 | 5 if y => println!("yes"),
_ => println!("no"),
}

// 伪代码表示
IF y AND (x IN List[4, 5])