Added items
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
pub module trait_test;
|
||||
|
||||
trait Speak {
|
||||
fn String speak(*self);
|
||||
}
|
||||
|
||||
trait Greet : Speak {
|
||||
fn String greet(*self);
|
||||
}
|
||||
|
||||
struct Dog {
|
||||
name String,
|
||||
}
|
||||
|
||||
impl Speak for Dog {
|
||||
fn String speak(*self) {
|
||||
"woof".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl Greet for Dog {
|
||||
fn String greet(*self) {
|
||||
"hello".to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn void test_trait_impl() {
|
||||
let d = Dog { name: "Rex".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
}
|
||||
|
||||
pub fn void test_trait_with_requirement() {
|
||||
let d = Dog { name: "Buddy".to_string() };
|
||||
assert_eq!(d.speak(), "woof");
|
||||
assert_eq!(d.greet(), "hello");
|
||||
}
|
||||
|
||||
pub fn void run_tests() {
|
||||
test_trait_impl();
|
||||
test_trait_with_requirement();
|
||||
}
|
||||
Reference in New Issue
Block a user