Chapter 9: Structs in Rust

Structs are a fundamental part of Rust's type system, allowing you to create complex data types that group together related values. They are similar to structs in C but offer additional features and safety guarantees. Structs are commonly used to model real-world entities and represent data with multiple related components (structures).

In this chapter, we'll explore:

  • Defining structs
  • Instantiating and using structs
  • Field initialization and access
  • Struct update syntax
  • Tuple structs
  • Unit-like structs
  • Methods and associated functions
  • The impl block
  • The self parameter
  • Getters and setters
  • Structs and ownership
  • Structs with references and lifetimes
  • Generic structs
  • Comparing Rust structs with OOP concepts
  • Derived traits