Skip to content

Instantly share code, notes, and snippets.

@Aatch
Last active December 16, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aatch/5428503 to your computer and use it in GitHub Desktop.
Save Aatch/5428503 to your computer and use it in GitHub Desktop.
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