Created
October 13, 2017 15:58
-
-
Save kmizu/1fe453564a9267ad7660e68851dfae07 to your computer and use it in GitHub Desktop.
Returns closure which is FnOnce
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 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