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!("其他"),
}
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"),
}