Created
August 6, 2018 12:07
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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