/******************************************************************************
Online Rust Compiler.
Code, Compile, Run and Debug Rust program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
trait HasWeight {
fn weight(&self) -> u32;
}
struct Cat {}
impl HasWeight for Cat {
fn weight(&self) -> u32 {
42
}
}
struct Dog {}
impl HasWeight for Dog {
fn weight(&self) -> u32 {
9001
}
}
struct CatDog {
cat: Cat,
dog: Dog,
}
impl HasWeight for CatDog {
fn weight(&self) -> u32 {
self.cat.weight() + self.dog.weight()
}
}
fn main() {
let dog = Dog{};
let cat = Cat{};
let catdog = CatDog{ cat: Cat{}, dog: Dog{} };
println!("{}", cat.weight());
println!("{}", dog.weight());
println!("{}", catdog.weight());
}