bots-garden/model-mellumpublic Fork
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/model-mellum.git
git clone ssh://git@rickub.com/bots-garden/model-mellum.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
🛟 Updated. a9e95e5 · on main · k33g · 2d ago
04-change-existing-person.md · 51 lines · 1.3 KBmarkdown
Blame HistoryOpen raw

How to Change the Values of an Existing Person in Your Rust Program

What the reader will be able to do: Modify the name and age values of an existing Human struct instance using mutability.

Prerequisites

  • Basic understanding of Rust structs and mutability (from previous tutorials)
  • Familiarity with let bindings and variable reassignment

Steps

  1. Create a mutable Human instance
    In src/main.rs, define a Human with mut:

    let mut person = Human {
        name: String::from("Alice"),
        age: 30,
    };
    
  2. Modify the values directly
    Change the fields using dot notation:

    person.name = String::from("Bob");
    person.age = 25;
    
  3. Print the updated values

    println!("Name: {}", person.name);
    println!("Age: {}", person.age);
    
  4. Compile and run
    Execute:

    cargo run
    

    Expected output:

    Name: Bob
    Age: 25
    

Pitfalls

  • Forgetting mut in the let binding (causes compilation errors)
  • Trying to modify immutable references (e.g., &Human instead of &mut Human)
  • Not passing &mut person to functions (requires mutable reference)

To go further

  • Learn about Rust's clone() method for deep copying structs
  • Explore how to implement From and Into traits for type conversion
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# How to Change the Values of an Existing Person in Your Rust Program

What the reader will be able to do: Modify the `name` and `age` values of an existing `Human` struct instance using mutability.

## Prerequisites
- Basic understanding of Rust structs and mutability (from previous tutorials)
- Familiarity with `let` bindings and variable reassignment

## Steps

1. **Create a mutable `Human` instance**  
   In `src/main.rs`, define a `Human` with `mut`:
   ```rust
   let mut person = Human {
       name: String::from("Alice"),
       age: 30,
   };
   ```

2. **Modify the values directly**  
   Change the fields using dot notation:
   ```rust
   person.name = String::from("Bob");
   person.age = 25;
   ```

3. **Print the updated values**  
   ```rust
   println!("Name: {}", person.name);
   println!("Age: {}", person.age);
   ```

4. **Compile and run**  
   Execute:
   ```bash
   cargo run
   ```
   Expected output:
   ```
   Name: Bob
   Age: 25
   ```

## Pitfalls
- Forgetting `mut` in the `let` binding (causes compilation errors)
- Trying to modify immutable references (e.g., `&Human` instead of `&mut Human`)
- Not passing `&mut person` to functions (requires mutable reference)

## To go further
- Learn about Rust's `clone()` method for deep copying structs
- Explore how to implement `From` and `Into` traits for type conversion