问题在于默认情况。
让我们考虑以下代码:
fn func(x: i64) {
match x {
0 => println!("Zero"),
1 => println!("One"),
_ => {
//How to get the value here not via repeating the matched expression ?
}
};
}
答案 0 :(得分:8)
假设您不想重复该表达式,因为它不仅比变量复杂,还可以将其绑定到变量:
fn func(x: i64) {
match <some complex expression> {
0 => println!("Zero"),
1 => println!("One"),
y => {
// you can use y here
}
};
}
这也可以作为默认情况,因为变量模式就像_
一样匹配所有内容。
_
在您不想使用该值时非常有用。