Created
May 9, 2021 08:45
-
-
Save rgb-24bit/7e51df32be0610d3bddbe3056192998c to your computer and use it in GitHub Desktop.
rust counter by closures
This file contains hidden or 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
| 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