Created
June 25, 2020 08:15
-
-
Save MikuroXina/33ece82dee5d161f4811d78199f78e35 to your computer and use it in GitHub Desktop.
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 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