Last active
December 16, 2015 11:38
-
-
Save Aatch/5428503 to your computer and use it in GitHub Desktop.
This file contains 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
struct OneShot<S, T> (S, ~fn (S) -> T); | |
impl<S, T> OneShot<S,T> { | |
fn run(~self) -> T { | |
let (s, f) = match self { | |
~OneShot(s, f) => (s,f) | |
}; | |
f(s) | |
} | |
} | |
fn one_shot<T,S>(e: S, f: ~fn(S) -> T) -> ~OneShot<S, T> { | |
~OneShot(e,f) | |
} | |
fn test(a:~int) -> ~OneShot<~int, ~str> { | |
let b = 2, c = ~10; | |
one_shot(a, |a| { | |
fmt!("%d", ~a + b + *c) | |
}) | |
} | |
fn main() { | |
let t = test(~10); | |
let r = t.run(); | |
//let _ = t.run(); this causes an error because of the move | |
io::println(fmt!("%?", r)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment