Skip to content

Instantly share code, notes, and snippets.

@greister
Forked from anonymous/playground.rs
Created April 23, 2016 17:49
Show Gist options
  • Save greister/54f4de551bfb9dc7ebeac9c52ab3cf8d to your computer and use it in GitHub Desktop.
Save greister/54f4de551bfb9dc7ebeac9c52ab3cf8d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
fn get_c() -> Box<FnMut(i32)> {
Box::new(|val: i32| {
println!("value --> {}", val);
})
}
pub fn set_c<F>(mut callback: F)
where F: FnMut(i32)
{
callback(100);
}
fn main() {
let mut cb = get_c();
set_c(|x| cb(x));
set_c(|val: i32| println!("value --> {}", val));
}
@greister
Copy link
Author

I had some discussion regarding passing closures returned from a function to another function as an argument --> http://is.gd/O1V2pj . I could make this
| work by using &mut F for closure parameter. Is there a way to make this work with out using reference?
0246 rtrt | pub fn set_c<F: ?Sized>(callback: &mut F)where F: FnMut(i32){} --> This works well
0247 rtrt | What if it is a 3rd party library and this is out the function is defined --> pub fn set_c(callback: F) where F: FnMut(i32)
0248 --> | Flat ([email protected]) has joined #rust-beginners
0249 @mbrubec+| rtrt: I guess you could call it as set_c(|x| cb(x))
0249 @mbrubec+| closures within closures...
0249 @mbrubec+| http://is.gd/Ai5ZTi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment