Created
April 6, 2015 08:08
-
-
Save bstrie/b72d8f96406fe1e75dae to your computer and use it in GitHub Desktop.
This file contains 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 make_counter = || { | |
let mut count = 0; | |
move || { let so_far = count; count += 1; so_far } | |
}; | |
let mut a = make_counter(); | |
let mut b = make_counter(); | |
println!("{} {}", a(), a()); | |
println!("{} {}", b(), b()); | |
} |
This file contains 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() -> Box<FnMut()->i32> { | |
let mut count = 0; | |
Box::new(move || { let so_far = count; count += 1; so_far }) | |
} | |
fn main() { | |
let mut a = make_counter(); | |
let mut b = make_counter(); | |
println!("{} {}", a(), a()); | |
println!("{} {}", b(), b()); | |
} |
This file contains 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() -> FnMut()->i32 { | |
let mut count = 0; | |
move || { let so_far = count; count += 1; so_far } | |
} | |
fn main() { | |
let mut a = make_counter(); | |
let mut b = make_counter(); | |
println!("{} {}", a(), a()); | |
println!("{} {}", b(), b()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment