Curriculum Traits & generics Derive and common traits
Derive and common traits
Rust leans on a small set of common traits: Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, and Hash. Derive is the idiomatic way to ask the compiler for boring implementations. Planned exercises: 1. choose #[derive(Debug)]. 2. recognise derived equality. 3. choose Clone vs Copy. 4. fill a derive list. 5. fill a Debug print. 6. write a comparable struct. 7. write a default value. 8. translate TS equality assumptions. 9. fix a HashMap key type that lacks the required traits.
Exercises
9 ready
01
pick one
A struct Point should be printable via the {:?} debug
02
pick one
A struct should compare with == (structural equality). Pick
03
pick one
When should a struct derive both Clone AND Copy (and not
04
fill blanks
Fill the trait list inside the derive attribute so the struct
05
type one line
Fill the line that prints the Point value p using DEBUG
06
write a program
Define struct Point { x: i32, y: i32 } with derives for
07
write a program
Define struct Counter { count: i32 } deriving Default
08
write a program
Translate the TS equality check. Two Point values with
09
write a program
This program fails because Key cannot be used as a