Skip to content

Instantly share code, notes, and snippets.

@MaikKlein
Created July 6, 2014 09:45
Show Gist options
  • Save MaikKlein/0c9bca157e4262b998b8 to your computer and use it in GitHub Desktop.
Save MaikKlein/0c9bca157e4262b998b8 to your computer and use it in GitHub Desktop.
use std::string::String;
trait MyToStr {
fn my_to_str(s: &Self) -> String;
}
struct Foo {
name : String
}
impl MyToStr for Foo {
fn my_to_str(s : &Foo) -> String {
s.name.clone() + " from Foo"
}
}
struct Bar {
name : String
}
impl MyToStr for Bar {
fn my_to_str(s : &Bar) -> String {
s.name.clone() + " from Bar"
}
}
fn main(){
let f = Foo {name : String::from_str("Foo")};
let b = Bar {name : String::from_str("Bar")};
let fs = MyToStr::my_to_str(&f);
let bs = MyToStr::my_to_str(&b);
println!("String is {}", fs);
println!("String is {}", bs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment