Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created June 7, 2019 00:21
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 rust-play/3adb9085caf9757f741b14a0c8c00812 to your computer and use it in GitHub Desktop.
Save rust-play/3adb9085caf9757f741b14a0c8c00812 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
let all_eq = vec![2,2,2,2];
let not_eq = vec![1,6,5,1,6];
assert!(is_all_same(all_eq));
assert_eq!(is_all_same(not_eq), false);
}
fn is_all_same(vec: Vec<usize>) -> bool {
vec.iter().fold((true, None), {|acc, elem| {
if let Some(prev) = acc.1 {
(acc.0 && (prev == elem), Some(elem))
} else {
(true, Some(elem))
}
}
}).0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment