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
02-human-struct.md · 58 lines · 1.4 KBmarkdown
Blame HistoryOpen raw

How to Add a Human Struct to Your Rust Program

What the reader will be able to do: Define a Human struct with name and age fields and use it in a Rust program.

Prerequisites

  • Basic Rust program structure (from previous tutorial)
  • Understanding of fn main() and println!

Steps

  1. Define the Human struct
    Open src/main.rs and replace its contents with:

    struct Human {
        name: String,
        age: u32,
    }
    
    fn main() {
        let person = Human {
            name: String::from("Alice"),
            age: 30,
        };
    
        println!("Name: {}", person.name);
        println!("Age: {}", person.age);
    }
    
  2. Compile and run
    Run:

    cargo run
    

    Expected output:

    Name: Alice
    Age: 30
    
  3. Modify the struct fields
    Change the name and age values in main():

    let person = Human {
        name: String::from("Bob"),
        age: 25,
    };
    

    Re-run cargo run to see the updated output.

Pitfalls

  • Forgetting to use String::from() for string literals (causes compilation errors)
  • Using i32 instead of u32 for age (Rust requires unsigned integers for non-negative values)
  • Not initializing struct fields in the correct order

To go further

  • Learn about Rust's Option and Result types for handling missing data
  • Explore how to implement methods on structs using impl blocks
 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
# How to Add a Human Struct to Your Rust Program

What the reader will be able to do: Define a `Human` struct with `name` and `age` fields and use it in a Rust program.

## Prerequisites
- Basic Rust program structure (from previous tutorial)
- Understanding of `fn main()` and `println!`

## Steps

1. **Define the Human struct**  
   Open `src/main.rs` and replace its contents with:
   ```rust
   struct Human {
       name: String,
       age: u32,
   }

   fn main() {
       let person = Human {
           name: String::from("Alice"),
           age: 30,
       };

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

2. **Compile and run**  
   Run:
   ```bash
   cargo run
   ```
   Expected output:
   ```
   Name: Alice
   Age: 30
   ```

3. **Modify the struct fields**  
   Change the `name` and `age` values in `main()`:
   ```rust
   let person = Human {
       name: String::from("Bob"),
       age: 25,
   };
   ```
   Re-run `cargo run` to see the updated output.

## Pitfalls
- Forgetting to use `String::from()` for string literals (causes compilation errors)
- Using `i32` instead of `u32` for age (Rust requires unsigned integers for non-negative values)
- Not initializing struct fields in the correct order

## To go further
- Learn about Rust's `Option` and `Result` types for handling missing data
- Explore how to implement methods on structs using `impl` blocks