Skip to content

Instantly share code, notes, and snippets.

@rgb-24bit
Created May 9, 2021 08:45
Show Gist options
  • Select an option

  • Save rgb-24bit/7e51df32be0610d3bddbe3056192998c to your computer and use it in GitHub Desktop.

Select an option

Save rgb-24bit/7e51df32be0610d3bddbe3056192998c to your computer and use it in GitHub Desktop.
rust counter by closures
fn main() {
let mut incr = make_counter(0, 2);
for _ in 1..10 {
println!("{}", incr());
}
}
fn make_counter(mut base: i32, step: i32) -> Box<dyn FnMut() -> i32> {
return Box::new(move || {
base += step;
base
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment