الانتقال إلى المحتوى الرئيسي
eLearner.app
الوحدة 4 · الدرس 1 من 27/14 في الدورة~12 min
دروس الوحدة (1/2)

تعريف ومثيل الهياكل

Structs (or structures) in Rust are custom data types that allow you to group multiple related values together. They are similar to objects or classes in other languages, but without inheritance.

Defining a Struct

To define a struct, we use the struct keyword followed by the name in PascalCase. Inside, we define the names and types of the fields:

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

Creating an Instance

To use a struct, we must create an instance by filling in all the specified fields:

Code
fn main() {
    let user1 = User {
        email: String::from("alice@example.com"),
        username: String::from("alice123"),
        active: true,
        sign_in_count: 1,
    };

    // Access fields using dot notation
    println!("Is user {} active? {}", user1.username, user1.active);
}

Struct Mutability

In Rust, the entire instance must be marked as mutable (mut) if you want to modify any of its fields. Rust does not allow marking only certain fields as mutable:

Code
fn main() {
    let mut user1 = User {
        email: String::from("alice@example.com"),
        username: String::from("alice123"),
        active: true,
        sign_in_count: 1,
    };

    user1.active = false; // Valid because user1 is mutable!
}

Tuple Structs and Unit-Like Structs

Rust also supports two other struct formats besides classic named-field structs:

  1. Tuple Structs: They have an associated name but the fields do not have individual names, only types. They are useful for defining and differentiating specific types:
    Code
    struct Point(i32, i32, i32);
    let origin = Point(0, 0, 0);
    
  2. Unit-Like Structs: They do not define any fields. They behave like the () unit type. They are useful when you need to implement behaviors (traits) on a type without storing any data in it.

Try it yourself

تمرين#rust.m4.l1.e1
المحاولات: 0جارٍ التحميل…

Define the User struct above main with fields: username (String type) and active (bool type).

جارٍ تحميل المحرر…
إظهار التلميح

Write `struct User { username: String, active: bool }`before`main`.

الحل متاح بعد 3 من المحاولات

تمرين#rust.m4.l1.e2
المحاولات: 0جارٍ التحميل…

Modify user1 to make it mutable (let mut user1) and change the active field to false before printing.

جارٍ تحميل المحرر…
إظهار التلميح

Add `mut`to`let user1`to make it`let mut user1`, then insert `user1.active = false;`.

الحل متاح بعد 3 من المحاولات

تمرين#rust.m4.l1.e3
المحاولات: 0جارٍ التحميل…

Define a tuple struct named Color containing three i32 fields. In main, instantiate Color with the values 255, 0, and 100 assigning it to the variable my_color, then print its first element (red value) using println!.

جارٍ تحميل المحرر…
إظهار التلميح

Define the tuple struct with `struct Color(i32, i32, i32);`. In main, instantiate it as `let my_color = Color(255, 0, 100);`and print the red value with`my_color.0`.

الحل متاح بعد 3 من المحاولات