Curriculum Data modeling Structs and impl blocks
Structs and impl blocks
Rust separates data declarations from method implementations. A struct names fields; an impl block attaches constructors and methods. Planned exercises: 1. choose struct declaration syntax. 2. recognise named-field literals. 3. choose Self in a constructor. 4. fill an impl block. 5. fill a &self method. 6. write a constructor. 7. write a read-only method. 8. translate a TS object with methods. 9. fix an accidental move out of self.
Exercises
9 ready
01
pick one
Declare a Point with x and y as f64 fields. Pick the
02
pick one
Given struct Point { x: f64, y: f64 }, pick the expression
03
pick one
Inside impl Point, a constructor fn new(x: f64, y: f64) returns
04
fill blanks
Fill the keyword that attaches methods to a struct. Goes
05
fill blanks
Fill the receiver for a method that READS the rectangle without
06
write a program
Define a struct Point with f64 fields x and y, and an
07
write a program
Define a struct Rectangle with f64 fields width and
08
write a program
Translate the TypeScript class to Rust. A Counter starts at
09
write a program
This method tries to RETURN the name out of self: