Created
June 11, 2016 08:46
-
-
Save lotabout/60a0b393323357ba164b78ec7e93f1d1 to your computer and use it in GitHub Desktop.
Rust closure as mutable callback.
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
use std::thread; | |
fn do_something<'a>(mut callback: Box<FnMut() + 'a>) { | |
// check http://stackoverflow.com/questions/35651279/error-closure-may-outlive-the-current-function-but-it-will-not-outlive-it | |
// about why adding 'a will work | |
callback(); | |
} | |
fn main() { | |
let mut ret = 0; | |
do_something(Box::new(|| {ret += 1;})); | |
println!("ret = {}", ret); // <- 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment