Trait Objects

https://doc.rust-lang.org/book/trait-objects.html

trait Foo {
    fn method(&self) -> String;
}

impl Foo for u8 {
    fn method(&self) -> String { format!("u8: {}", *self) }
}

impl Foo for String {
    fn method(&self) -> String { format!("string: {}", *self) }
}

fn do_something<T: Foo>(x: T) {
    x.method();
}

fn main() {
    let x = 5u8;
    let y = "Hello".to_string();

    do_something(x); // u8向けのmethod関数が呼ばれる
    do_something(y); // String向けのmethod関数が呼ばれる
}