Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Rust Dictionary

  1. Home
  2. Rust Dictionary
  3. struct / Field Definitions

struct / Field Definitions

A struct in Rust is a data type that groups multiple values together. There are three kinds: a regular struct with named fields, a tuple struct with unnamed fields, and a unit struct with no fields.

Syntax

// Regular struct (fields have names).
struct Point {
    x: f64,
    y: f64,
}

// Tuple struct (fields have no names).
struct Color(u8, u8, u8);

// Unit struct (no fields).
struct Marker;

// Creating instances.
let p = Point { x: 1.0, y: 2.5 };
let c = Color(255, 128, 0);
let m = Marker;

// Accessing fields.
println!("{}", p.x);   // 1.0
println!("{}", c.0);   // 255 (tuple struct fields are accessed by index)

Struct Kinds

KindDescription
struct Name { field: Type, ... }A regular struct with named fields.
struct Name(Type, Type, ...);A tuple struct with unnamed fields.
struct Name;A unit struct with no fields (used as trait markers, etc.).
let mut s = Struct { ... }Adding mut makes all fields mutable.
Field shorthandWhen a variable name matches a field name, you can write { x, y } instead of { x: x, y: y }.
struct Struct2 { .., ..struct1 }Copies the remaining fields from another instance (struct update syntax).

Sample Code

// A struct representing user information.
struct User {
    username: String,
    email: String,
    age: u32,
    active: bool,
}

// Examples of tuple structs.
struct Point(f64, f64);
struct Meters(f64);

fn build_user(email: String, username: String) -> User {
    User {
        // Field shorthand: variable names match field names, so they can be omitted.
        email,
        username,
        age: 20,
        active: true,
    }
}

fn main() {
    // Create an instance.
    let user1 = User {
        username: String::from("Alice"),
        email: String::from("alice@example.com"),
        age: 28,
        active: true,
    };

    println!("User: {}", user1.username);
    println!("Email: {}", user1.email);
    println!("Age: {}", user1.age);

    // Use mut to modify fields (the entire struct becomes mutable).
    let mut user2 = build_user(
        String::from("bob@example.com"),
        String::from("Bob"),
    );
    user2.age = 30;
    println!("Bob's age after update: {}", user2.age);

    // Struct update syntax: create a new instance by changing only some fields.
    let user3 = User {
        email: String::from("carol@example.com"),
        username: String::from("Carol"),
        ..user1  // Copy the remaining fields from user1.
    };
    println!("user3 age: {}", user3.age);    // 28 (copied from user1)
    println!("user3 active: {}", user3.active); // true

    // Using tuple structs.
    let p = Point(3.0, 4.0);
    let d = Meters(5.2);
    println!("Point: ({}, {})", p.0, p.1);
    println!("Distance: {} m", d.0);

    // Tuple structs also support destructuring.
    let Point(x, y) = p;
    println!("x={}, y={}", x, y);
}

Notes

Rust structs do not implement traits such as Debug by default. To print debug output with println!("{:?}", s), you need to add the #[derive(Debug)] attribute.

With the struct update syntax (..other), fields of types like String are moved. After the move, those fields can no longer be accessed from the original instance.

To add methods to a struct, see impl / Method Definitions. For automatically deriving trait implementations, see #[derive(Debug, Clone, PartialEq)].

If you find any errors or copyright issues, please .