Created
November 14, 2022 11:38
-
-
Save sshine/3cbf3ce10a4c04c104716359f020a371 to your computer and use it in GitHub Desktop.
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
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