Defining and Using Structs

Structs in Rust are used to create custom data types that group related values.

Defining a Struct

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

Creating an Instance

let user1 = User {
    username: String::from("harshal123"),
    email: String::from("harshal@example.com"),
    active: true,
};

Accessing Fields

println!("Username: {}", user1.username);

Mutable Struct

let mut user2 = user1;
user2.active = false;
← PrevNext →