Skip to content

Instantly share code, notes, and snippets.

@lotabout
Created June 11, 2016 08:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotabout/60a0b393323357ba164b78ec7e93f1d1 to your computer and use it in GitHub Desktop.
Save lotabout/60a0b393323357ba164b78ec7e93f1d1 to your computer and use it in GitHub Desktop.
Rust closure as mutable callback.
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