Inheritance with traits
Traits can also inherit from other traits, indicated with the : operator:
trait Trait1 : SuperTrait
Look at the following code, where trait Monster inherits from trait SuperMonster. In such a case any type that implements Monster must also implement SuperMonster, in this specific case its super1() method:
// see code in Chapter 6/code/super_traits.rs
struct Zombie { health: u32, damage: u32 }
trait SuperMonster {
fn super1(&self);
}
trait Monster : SuperMonster {
fn new(hlt: u32, dam: u32) -> Self;
fn attack(&self);
fn noise(&self) -> &'static str;
}
impl SuperMonster for Zombie {
fn super1(&self) {
println!("I am a SuperMonster");
}
}
impl Monster for Zombie {
fn new(mut h: u32, d: u32) -> Zombie {
if h > 100 { h = 100; }
Zombie { health: h, damage: d }
}
fn attack(&self) {
println!("The Zombie bites! Your health lowers with {} damage...