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
03-human-struct-change.md · 72 lines · 1.8 KBmarkdown
Blame HistoryOpen raw

How to Change the Values of a Person in Your Rust Program

What the reader will be able to do: Modify the name and age values of a Human struct at runtime.

Prerequisites

  • Basic understanding of Rust structs and fn main() (from previous tutorials)
  • Familiarity with String and u32 types

Steps

  1. Modify the struct initialization in main()
    Open src/main.rs and change the person struct values:

    let person = Human {
        name: String::from("Charlie"),
        age: 35,
    };
    
  2. Add user input to dynamically change values
    Replace the main() function with:

    use std::io;
    
    fn main() {
        println!("Enter your name:");
        let mut name = String::new();
        io::stdin()
            .read_line(&mut name)
            .expect("Failed to read line");
    
        println!("Enter your age:");
        let mut age_input = String::new();
        io::stdin()
            .read_line(&mut age_input)
            .expect("Failed to read line");
    
        let age: u32 = age_input.trim().parse().expect("Please enter a valid number");
    
        let person = Human {
            name: name.trim().to_string(),
            age,
        };
    
        println!("Name: {}", person.name);
        println!("Age: {}", person.age);
    }
    
  3. Compile and run
    Run:

    cargo run
    

    Expected interaction:

    Enter your name:
    David
    Enter your age:
    40
    Name: David
    Age: 40
    

Pitfalls

  • Forgetting to call .trim() on input strings (leaves newline characters in the output)
  • Not handling parsing errors with .expect() (causes crashes on invalid input)
  • Using String instead of &str for input (requires explicit conversion)

To go further

  • Learn about Rust's match expressions for validating user input
  • Explore how to create multiple Human instances in a loop
 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# How to Change the Values of a Person in Your Rust Program

What the reader will be able to do: Modify the `name` and `age` values of a `Human` struct at runtime.

## Prerequisites
- Basic understanding of Rust structs and `fn main()` (from previous tutorials)
- Familiarity with `String` and `u32` types

## Steps

1. **Modify the struct initialization in `main()`**  
   Open `src/main.rs` and change the `person` struct values:
   ```rust
   let person = Human {
       name: String::from("Charlie"),
       age: 35,
   };
   ```

2. **Add user input to dynamically change values**  
   Replace the `main()` function with:
   ```rust
   use std::io;

   fn main() {
       println!("Enter your name:");
       let mut name = String::new();
       io::stdin()
           .read_line(&mut name)
           .expect("Failed to read line");

       println!("Enter your age:");
       let mut age_input = String::new();
       io::stdin()
           .read_line(&mut age_input)
           .expect("Failed to read line");

       let age: u32 = age_input.trim().parse().expect("Please enter a valid number");

       let person = Human {
           name: name.trim().to_string(),
           age,
       };

       println!("Name: {}", person.name);
       println!("Age: {}", person.age);
   }
   ```

3. **Compile and run**  
   Run:
   ```bash
   cargo run
   ```
   Expected interaction:
   ```
   Enter your name:
   David
   Enter your age:
   40
   Name: David
   Age: 40
   ```

## Pitfalls
- Forgetting to call `.trim()` on input strings (leaves newline characters in the output)
- Not handling parsing errors with `.expect()` (causes crashes on invalid input)
- Using `String` instead of `&str` for input (requires explicit conversion)

## To go further
- Learn about Rust's `match` expressions for validating user input
- Explore how to create multiple `Human` instances in a loop