add slice

This commit is contained in:
2020-01-30 22:02:39 +08:00
parent 1d24f1d21b
commit 8d870b6ee5

View File

@@ -0,0 +1,31 @@
// https://rust.cc/article?id=fa12f620-5dbd-4e61-a8c1-2529e71bc96b
fn main() {
greet(&[]);
greet(&["alice"]);
greet(&["alice", "bob"]);
greet(&["alice", "bob", "tom"]);
let arr = [1, 2, 3];
println!("{}", match arr {
[_, _, 3] => "Ends with 3",
[_, _, _] => "Ends with something else",
});
}
fn greet(people: &[&str]) {
match people {
[] => println!("No noe here!"),
[one] => println!("Has one: {}", one),
[one, two] => println!("Has two: {} and {}", one, two),
many => println!("Has many: {:?}", many),
}
}
// UNSTABLE FEATURE:
// fn first<T>(xs: &[T]) -> Option<&T> {
// match xs {
// [x, ..] => Some(x),
// [] => None,
// }
// }