Files
simple-rust-tests/single_file_tests/slice.rs
2020-01-30 22:02:39 +08:00

32 lines
744 B
Rust

// 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,
// }
// }