Created
June 12, 2021 06:39
-
-
Save softprops/54bc5f2dbba6410e9f120449c3b2cbca to your computer and use it in GitHub Desktop.
This file contains hidden or 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
use rand; // 0.8.3 | |
use rand::Rng; | |
use std::iter::successors; | |
fn non_repeating<T: std::cmp::PartialEq + Clone>(src: &[T]) -> impl Iterator<Item = &T> + '_ { | |
let mut rng = rand::thread_rng(); | |
successors(Some(&src[rng.gen_range(0..src.len())]), move |n| { | |
let mut next = &src[rng.gen_range(0..src.len())]; | |
while *next == **n { | |
next = &src[rng.gen_range(0..src.len())]; | |
} | |
Some(next) | |
}) | |
} | |
fn main() { | |
for i in non_repeating(&["a", "b"]).take(10) { | |
println!("{}", i) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment