Is there anyway to implement inheritance in Rust? I've read Extending Traits, as the comment said:
Rust has trait inheritance, it looks like this:
pub trait A {}
pub trait B: A {}
So I tried to implement this Python code in Rust:
class Animal(object):
@classmethod
def howl(cls):
print("ao ao")
class Tiger(Animal):
@classmethod
def find_food(cls):
print("find food")
a = Tiger()
a.howl()
a.find_food()
Rust way: lib.rs
pub trait Animal {
fn howl(&self) { println!("ao ao"); }
}
pub trait Tiger: Animal {
fn find_food(&self) { println!("find food"); }
}
impl<'a> Animal for &'a str {}
impl<'a> Tiger for &'a str {}
main.rs
extern crate colorful;
use colorful::Tiger;
fn main() {
"da".find_food();
// "da".howl();
}
If I want to use howl
I must import Animal
trait, and I don't think this is inheritance, is there a way to import one trait in order to use all method of this trait and subtrait? How can I organize code like this?