Skip to content

Instantly share code, notes, and snippets.

@sophiajt
Created March 16, 2016 20:31
Show Gist options
  • Save sophiajt/9682e655a3a3e2f470dc to your computer and use it in GitHub Desktop.
Save sophiajt/9682e655a3a3e2f470dc to your computer and use it in GitHub Desktop.
use std::any::Any;
#[derive(Debug)]
struct Engine {
x: i32
}
trait FnRegister<RetVal, Args> {
fn register<T: Fn(Args)->RetVal>(&mut self, name: &str, f: T);
}
impl FnRegister<i32, (i32, i32)> for Engine {
fn register<T: Fn(i32, i32)->i32>(&mut self, name: &str, f: T) {
self.x += 2;
}
}
fn add(x:i32, y: i32) -> i32 {
x + 10
}
fn main() {
let mut engine = Engine { x: 0 };
engine.register("bar", add);
println!("Engine: {:?}", engine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment