diff --git a/single_file_tests/slice.rs b/single_file_tests/slice.rs new file mode 100644 index 0000000..71f1843 --- /dev/null +++ b/single_file_tests/slice.rs @@ -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(xs: &[T]) -> Option<&T> { +// match xs { +// [x, ..] => Some(x), +// [] => None, +// } +// }