Skip to content

Instantly share code, notes, and snippets.

@flaper87
Created January 29, 2014 19:51
Show Gist options
  • Save flaper87/8695610 to your computer and use it in GitHub Desktop.
Save flaper87/8695610 to your computer and use it in GitHub Desktop.
use std::io;
fn echo<T>(x:T) { io::println(format!("{}", x)); }
pub trait OpInt<'a> { fn call(&'a self, int, int) -> int; }
impl<'a> OpInt<'a> for &'a |int, int| -> int {
fn call(&self, a:int, b:int) -> int {
echo("I dont wanna die!");
(*self)(a, b)
}
}
fn squarei<'a>(x:int, op:&'a OpInt) -> int { op.call(x, x) }
fn muli(x:int, y:int) -> int {
echo("You will never get here.");
x * y
}
fn main() {
echo("Entered main");
let f = |x,y|muli(x,y);
{
let g = &f;
let h = g as &OpInt;
let r = squarei(3, h); echo(r);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment