Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Created June 25, 2020 08:15
Show Gist options
  • Select an option

  • Save MikuroXina/33ece82dee5d161f4811d78199f78e35 to your computer and use it in GitHub Desktop.

Select an option

Save MikuroXina/33ece82dee5d161f4811d78199f78e35 to your computer and use it in GitHub Desktop.
fn make_counter<T, Identity>(init: T, step: Identity) -> impl FnMut() -> T
where
Identity: Clone,
T: std::ops::AddAssign<Identity> + Clone,
{
let mut counter = init;
move || {
let to_return = counter.clone();
counter += step.clone();
to_return
}
}
fn main() {
let mut counter = make_counter(0, 1);
println!("{}", counter());
println!("{}", counter());
println!("{}", counter());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment