Syntax: fn type name
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
pub module trait_test;
|
||||
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user