Advertisement
Guest User

Untitled

a guest
Oct 4th, 2021
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.93 KB | None | 0 0
  1. match slice {
  2.     [] => println!("slice is empty"),
  3.     [one] => println!("single element {}", one),
  4.     [head, tail @ ..] => println!("head={} tail={:?}", head, tail),
  5. }
  6.  
  7. match slice {
  8.     // Ignore everything but the last element, which must be "!".
  9.     [.., "!"] => println!("!!!"),
  10.  
  11.     // `start` is a slice of everything except the last element, which must be "z".
  12.     [start @ .., "z"] => println!("starts with: {:?}", start),
  13.  
  14.     // `end` is a slice of everything but the first element, which must be "a".
  15.     ["a", end @ ..] => println!("ends with: {:?}", end),
  16.  
  17.     rest => println!("{:?}", rest),
  18. }
  19.  
  20. if let [.., penultimate, _] = slice {
  21.     println!("next to last is {}", penultimate);
  22. }
  23.  
  24. // Rest patterns may also be used in tuple and tuple struct patterns.
  25. match tuple {
  26.     (1, .., y, z) => println!("y={} z={}", y, z),
  27.     (.., 5) => println!("tail must be 5"),
  28.     (..) => println!("matches everything else"),
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement