SHARE
TWEET

Untitled

a guest Jun 18th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     let empty = vec![];
  3.     let all_eq = vec![2,2,2,2];
  4.     let not_eq = vec![1,6,5,1,6];
  5.    
  6.     assert!(is_all_same(&empty));
  7.     assert!(is_all_same2(&empty));
  8.     assert!(is_all_same3(&empty));
  9.     assert!(is_all_same(&all_eq));
  10.     assert!(is_all_same2(&all_eq));
  11.     assert!(is_all_same3(&all_eq));
  12.     assert_eq!(is_all_same(&not_eq), false);
  13.     assert_eq!(is_all_same2(&not_eq), false);
  14.     assert_eq!(is_all_same3(&not_eq), false);
  15. }
  16.  
  17.  
  18. fn is_all_same(arr: &[usize]) -> bool {
  19.     if arr.is_empty() {
  20.         return true;
  21.     }
  22.     let first = arr[0];
  23.     arr.iter().all(|&item| item == first)
  24. }
  25.  
  26. fn is_all_same2(arr: &[usize]) -> bool {
  27.     arr.iter().min() == arr.iter().max()
  28. }
  29.  
  30. fn is_all_same3<T: PartialEq>(arr: &[T]) -> bool {
  31.     arr.windows(2).all(|w| w[0] == w[1])
  32. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top