Files
mist/mist-test/src/items/trait_test.mist
T
2026-06-05 20:06:38 +02:00

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();
}