Created
July 6, 2014 09:45
-
-
Save MaikKlein/0c9bca157e4262b998b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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