Syntax: fn type name

This commit is contained in:
2026-06-16 03:29:01 +02:00
parent eea72f5bb3
commit aadccb7f38
15 changed files with 9 additions and 842 deletions
-41
View File
@@ -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();
}