Created
June 7, 2019 00:21
-
-
Save rust-play/3adb9085caf9757f741b14a0c8c00812 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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 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