Skip to content

Instantly share code, notes, and snippets.

@kmizu
Created October 13, 2017 15:58
Show Gist options
  • Save kmizu/1fe453564a9267ad7660e68851dfae07 to your computer and use it in GitHub Desktop.
Save kmizu/1fe453564a9267ad7660e68851dfae07 to your computer and use it in GitHub Desktop.
Returns closure which is FnOnce
fn test() -> Box<MFunBox<i32, Box<Vec<i32>>>> {
let mut v = Box::new(vec![1, 2, 3]);
return Box::new(move |x: i32|{ v.push(x); v});
}
trait MFunBox<A, B> {
fn mcall(self: Box<Self>, arg: A) -> B;
}
impl<A, B, T:FnOnce(A) -> B> MFunBox<A, B> for T {
fn mcall(self: Box<Self>, arg: A) -> B {
(*self)(arg)
}
}
fn main() {
let f1 = test();
let v1 = f1.mcall(4);
println!("{:?}", v1);
let f2 = test();
let v2 = f2.mcall(5);
println!("{:?}", v2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment