Skip to main content
Module 4 · Lesson 1 of 27/24 in the course~15 min
Module lessons (1/2)

Define Struct, Tuple Struct and #[derive(Debug)]

Lesson preview5 hands-on exercises readyCourse Rust · Module 4 — Structures and Methods

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 struct nominals 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

  1. 11. Nominal structs and field init shorthand
  2. 22. Struct Update Syntax (..base)
  3. 33. Tuple Struct and Newtype Pattern
  4. 44. Debug Formatting: {:?}, {:#?} and dbg!
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 4 — Structures and Methods

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.