0PricingLogin
Learn Rust Coding · Lesson

Defining and Using Structs

Create custom data structures with structs, including tuple structs and unit-like structs, to group related data.

Meet Structs: Custom Data Types

In Rust, structs (short for structures) are a way to create custom data types that let you name and package together multiple related values into a meaningful group.

Think of a struct as a blueprint for a user, a color, or a point in space. Instead of having separate variables for a user's name, email, and age, you can put them all inside a User struct.

This helps you organize your data logically and make your code easier to read and maintain.

Defining a Classic Struct

The most common type of struct is a classic struct, where you name each piece of data, called a field. Here's how you define one:

struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

fn main() {
    // This code only defines the struct blueprint.
    // No actual user has been created yet!
    println!("Struct 'User' blueprint defined.");
}

All lessons in this course

  1. Defining and Using Structs
  2. Enums for Custom Types
  3. Powerful Pattern Matching
← Back to Learn Rust Coding