-
-
Save greister/54f4de551bfb9dc7ebeac9c52ab3cf8d to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or 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 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)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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