Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Created August 6, 2018 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kerollmops/f47eb506db59162cbb6a269bf13b7e14 to your computer and use it in GitHub Desktop.
Save Kerollmops/f47eb506db59162cbb6a269bf13b7e14 to your computer and use it in GitHub Desktop.
A function to test if all values of a slice are equal (to the first element).
fn all_eq<T: Eq>(slice: &[T]) -> bool {
match slice.split_first() {
Some((first, others)) => others.iter().all(|x| x == first),
None => true,
}
}
#[test]
fn all_eq_easy_eq() {
assert!(all_eq(&[1, 1, 1, 1, 1]));
}
#[test]
fn all_eq_easy_ne() {
assert!(all_eq(&[0, 1, 1, 1, 1]) == false);
}
#[test]
fn all_eq_easy_empty_eq() {
assert!(all_eq::<i32>(&[]));
}
#[test]
fn all_eq_easy_one_val_eq() {
assert!(all_eq(&[1]));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment