Module lessons (1/2)
Define Struct, Tuple Struct and #[derive(Debug)]
Declare struct, construct instances with shorthand field init, update ..other syntax, instance mutability, and debug formatting {:?}.
What you'll learn in this lesson
- Group related heterogeneous data into
structnominals with explicit types. - Simplify construction with field init shorthand and partial cloning (
..template). - Use tuple struct for safe newtypes and inspect instances with
#[derive(Debug)]({:?}/{:#?}).
Overview
The struct in Rust collect related fields into a contiguous block in memory with no hidden vtables or class headers. Unlike anonymous tuples, each field has a clear name verified by the compiler.
If the name of the local variable matches that of the struct field, you can omit : variabile:
Sections covered in this lesson
- 11. Nominal structs and field init shorthand
- 22. Struct Update Syntax (
..base) - 33. Tuple Struct and Newtype Pattern
- 44. Debug Formatting:
{:?},{:#?}anddbg! - 5Practical exercises
- 6Check for understanding
Unlock "Define Struct, Tuple Struct and #[derive(Debug)]" and the full Course Rust curriculum
Declare struct, construct instances with shorthand field init, update `..other` syntax, instance mutability, and debug formatting `{:?}`.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Course Rust plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.