A Mutable Counter Closure
10 points
Implement make_counter, which returns a closure that starts at 0 and returns one higher each time it's called. Because the closure mutates state it owns between calls, its return type is impl FnMut() -> i32, not impl Fn() -> i32.
Required signature
pub fn make_counter() -> impl FnMut() -> i32;
Visible tests
-
counts_up_from_one#[test] fn counts_up_from_one() { let mut c = make_counter(); assert_eq!(c(), 1); assert_eq!(c(), 2); assert_eq!(c(), 3); }
Your solution
Log in to submit a solution.