struct#

Basic usage of struct#

Let’s learn how to use Structs in Rust.
The struct Person { ~~~ } part in the following example defines a struct.
The Person struct has the fields name, age, and is_student, and each field has its type defined.

struct Person {
    name: String,
    age: i32,
    is_student: bool,
}

fn main() {
    let mut person = Person {
        name: String::from("semonan.com"),
        age: 12,
        is_student: true,
    };
    println!("{}, {}, {}", person.name, person.age, person.is_student);

    person.age = 13;
    println!("{}, {}, {}", person.name, person.age, person.is_student);
}

A struct must be instantiated to be used, as shown with let mut person = Person { ~~~ }.
In the example above, the instance is created as mutable so that the values of each field can be changed.
You can read the values of the struct with code like person.name, person.age, person.is_student.
You can change a value with code like person.age = 13;.

If the field name and the variable name are the same, you only need to code it once#

The let person = Person { ~~~ }; in the following example is the code that creates an instance.
Inside the { ~~~ }, values are assigned in the form age: 11.
In the line below, the code is written as name,. When assigning a value from a variable, if the field name and the variable name are the same, you only need to code it once.
Here, name is equivalent to name: name.

struct Person {
    age: i32,
    name: String,
}

fn main() {
    let name = String::from("semonan.com");

    let person = Person {
        age: 11,
        name,    // Same with --> name: name,
    };
    
    println!("{}, {}", 
        person.age,
        person.name
    );
}

Knowing the Struct Update Syntax is very convenient#

Let’s look at the let person_2 = Person { ~~~ }; part in the following example.
This is the code that creates person_2.
The name field is assigned a value directly with name: String::from("simon"),,
and the remaining fields are assigned the same values as person_1 using ..person_1.
This syntax becomes more useful as the number of fields in the struct increases.

struct Person {
    age: i32,
    name: String,
    weight: i32,
    is_student: bool,
}

fn main() {
    let person_1 = Person {
        age: 11,
        name: String::from("semonan.com"),
        weight: 99,
        is_student: true,
    };

    let person_2 = Person {
        name: String::from("simon"),
        ..person_1
    };
    
    println!("{}, {}, {}, {}", 
        person_2.age,
        person_2.name,
        person_2.weight,
        person_2.is_student
    );
}

Tuple Structs#

Let’s learn about Tuple structs through the following example.
A Tuple struct can be defined as struct Color (i32, i32, i32); as in the example, where the field names are omitted and only the types are specified.
The types of each field do not need to be the same.
An instance of a Tuple struct can be created with let mut my_color = Color(50, 100, 150);, where only values are specified since there are no field names.
To access the fields, you can read them with my_color.0 or write to them with my_color.0 = 10;.

struct Color (i32, i32, i32);

fn main() {
    let mut my_color = Color(50, 100, 150);
    
    println!("\n {}\n {}\n {}\n", 
        my_color.0,
        my_color.1,
        my_color.2
    );

    my_color.0 = 10;
}