add slice
This commit is contained in:
31
single_file_tests/slice.rs
Normal file
31
single_file_tests/slice.rs
Normal 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,
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user