切片模式

Minimum Rust version: 1.26

你有没有试过,用模式匹配去匹配切片的内容和结构? Rust 2018 将让你做到这一点。

例如,我们想要接受一个名单列表并回复问候语。使用切片模式,我们可以用以下方式轻松完成:

1. fn main() {
2. greet(&[]);
3. // output: Bummer, there's no one here :(
4. greet(&["Alan"]);
5. // output: Hey, there Alan! You seem to be alone.
6. greet(&["Joan", "Hugh"]);
7. // output: Hello, Joan and Hugh. Nice to see you are at least 2!
8. greet(&["John", "Peter", "Stewart"]);
9. // output: Hey everyone, we seem to be 3 here today.
10. }

12. fn greet(people: &[&str]) {
13. match people {
14. [] => println!("Bummer, there's no one here :("),
15. [only_one] => println!("Hey, there {}! You seem to be alone.", only_one),
16. [first, second] => println!(
17. "Hello, {} and {}. Nice to see you are at least 2!",
18. first, second
19. ),
20. _ => println!("Hey everyone, we seem to be {} here today.", people.len()),
21. }
22. }

现在,你不必检查长度了。

你也可以匹配 array 如下:

1. let arr = [1, 2, 3];

3. assert_eq!("ends with 3", match arr {
4. [_, _, 3] => "ends with 3",
5. [a, b, c] => "ends with something else",
6. });

更多的细节

穷举模式

在第一个例子中,注意匹配的 _ => ...。 如果开始匹配,那么将会匹配一切情况,所以有“穷尽所有模式”的处理方式。 如果我们忘记使用 _ => ... 或者 identifier => ... 模式,我们会得到如下的错误提醒:

1. error[E0004]: non-exhaustive patterns: `&[_, _, _]` not covered

如果我们再增加一项,我们将得到如下:

1. error[E0004]: non-exhaustive patterns: `&[_, _, _, _]` not covered

如此。

数组和精确的长度

在第二个例子中,数组是有固定长度的,我们需要匹配所有长度项,如果只匹配2,4项的话,会报错:

1. error[E0527]: pattern requires 2 elements but array has 3

和

1. error[E0527]: pattern requires 4 elements but array has 3

管道中

在切片模式方面,计划采用更先进的形式,但尚未稳定。要了解更多信息,请跟踪 the tracking issue。