Skip to content

Rust Structs — Defining, Instantiating, and Implementing Methods on Struct Types

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Rust Structs. We cover key concepts, practical examples, and best practices to help you master this topic.

Rust structs define custom data types with named fields, impl blocks for methods, tuple structs, and unit structs for different use cases.

What You'll Learn

  • Named field structs
  • Tuple structs
  • Unit structs
  • Methods and associated functions

Why It Matters

Structs are the foundation of Rust Data Modeling. DodaZIP uses structs for file metadata and archive entries. Firefox uses structs for DOM nodes.

Real-World Use

Data modeling, API request/response types, configuration structures, entity representation.

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

struct Color(i32, i32, i32);  // Tuple struct
struct UnitStruct;  // Unit struct

impl User {
    fn new(username: &str, email: &str) -> User {
        User {
            username: String::from(username),
            email: String::from(email),
            sign_in_count: 1,
            active: true,
        }
    }

    fn update_email(&mut self, email: &str) {
        self.email = String::from(email);
    }

    fn is_active(&self) -> bool {
        self.active
    }
}

fn main() {
    let mut user = User::new("alice", "alice@example.com");
    println!("User: {} ({})", user.username, user.email);
    println!("Active: {}", user.is_active());

    let black = Color(0, 0, 0);
    println!("RGB: {} {} {}", black.0, black.1, black.2);
}

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro