Curriculum Traits & generics Traits
Traits
Traits describe behavior a type provides. They feel partly like TypeScript interfaces, but implementation is explicit and coherence rules decide where impls may live. Planned exercises: 1. choose trait declaration syntax. 2. recognise impl Trait for Type. 3. choose a trait method receiver. 4. fill a trait method signature. 5. fill an impl block. 6. write a small trait and impl. 7. write a function that accepts a trait bound. 8. translate a TS interface plus class. 9. fix a missing trait import or missing impl error.
Exercises
9 ready
01
pick one
Declare a trait Greeter with one method greet(&self) -> String
02
pick one
Implement the Greeter trait for a struct Robot, returning
03
pick one
A trait method only READS the implementor's state — no
04
type one line
Inside trait Greeter { ... }, write the method signature for
05
type one line
Open an impl block that implements the Greeter trait for the
06
write a program
Declare a trait Greeter with one method
07
write a program
Reuse the Greeter trait + Robot impl. Add a function
08
write a program
Translate the TS interface + class pattern. Trait Speaker
09
write a program
This program fails with error[E0599]: no method named greet`