Skip to content

Instantly share code, notes, and snippets.

@Latrasis
Created March 6, 2017 19:04
Show Gist options
  • Save Latrasis/d6710a13000cb18e5efb2da2fdfa6536 to your computer and use it in GitHub Desktop.
Save Latrasis/d6710a13000cb18e5efb2da2fdfa6536 to your computer and use it in GitHub Desktop.
struct Animal {};
type Cat = Animal;
type Dog = Animal;
impl ToString for Animal {
fn to_string(&self) -> String {
"Animal".to_string()
}
}
impl ToString for Cat {
fn to_string(&self) -> String {
"Cat".to_string()
}
}
impl ToString for Dog {
fn to_string(&self) -> String {
"Dog".to_string()
}
}
fn main() {
let animal = Animal {};
// "Animal"
println!("{:}", animal);
// "Cat"
println!("{:}", animal as Cat);
// "Dog"
println!("{:}", animal as Dog);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment