Skip to content

Instantly share code, notes, and snippets.

@softprops
Created June 12, 2021 06:39
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 softprops/54bc5f2dbba6410e9f120449c3b2cbca to your computer and use it in GitHub Desktop.
Save softprops/54bc5f2dbba6410e9f120449c3b2cbca to your computer and use it in GitHub Desktop.
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