40 lines
655 B
Plaintext
40 lines
655 B
Plaintext
trait Speak {
|
|
fn speak(*self) String;
|
|
}
|
|
|
|
trait Greet : Speak {
|
|
fn greet(*self) String;
|
|
}
|
|
|
|
struct Dog {
|
|
name String,
|
|
}
|
|
|
|
impl Speak for Dog {
|
|
fn speak(*self) String {
|
|
"woof".to_string()
|
|
}
|
|
}
|
|
|
|
impl Greet for Dog {
|
|
fn greet(*self) String {
|
|
"hello".to_string()
|
|
}
|
|
}
|
|
|
|
pub fn test_trait_impl() {
|
|
let d = Dog { name: "Rex".to_string() };
|
|
assert_eq!(d.speak(), "woof");
|
|
}
|
|
|
|
pub fn test_trait_with_requirement() {
|
|
let d = Dog { name: "Buddy".to_string() };
|
|
assert_eq!(d.speak(), "woof");
|
|
assert_eq!(d.greet(), "hello");
|
|
}
|
|
|
|
pub fn run_tests() {
|
|
test_trait_impl();
|
|
test_trait_with_requirement();
|
|
}
|