Skip to content

Instantly share code, notes, and snippets.

@sshine
Created November 14, 2022 11:38
Show Gist options
  • Save sshine/3cbf3ce10a4c04c104716359f020a371 to your computer and use it in GitHub Desktop.
Save sshine/3cbf3ce10a4c04c104716359f020a371 to your computer and use it in GitHub Desktop.
use num_traits::{Zero, One};
use std::ops::{AddAssign};
fn enumerate_generic<T>() -> impl Iterator<Item = T>
where
T: Clone + Zero + One + AddAssign,
{
let mut curr = T::zero();
std::iter::repeat_with(move || {
let next = curr.clone();
curr += T::one();
next
})
}
fn main() {
let foos: Vec<u64> = enumerate_generic().take(10).collect();
println!("{:?}", foos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment