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. a1a9639 · on main · k33g · 2d ago
06-struct-method.md · 57 lines · 1.4 KBmarkdown
Blame HistoryOpen raw

How to Add a Greeting Method to the Human Structure

What the reader will be able to do: Define a method on the Human struct that prints a personalized greeting.

Prerequisites

  • Basic understanding of Rust structs and mutability (from previous tutorials)
  • Familiarity with fn main() and println!

Steps

  1. Define the Human struct
    Open src/main.rs and ensure you have:

    struct Human {
        name: String,
        age: u32,
    }
    
  2. Add a method to the Human struct
    Replace the main() function with:

    impl Human {
        fn greet(&self) {
            println!("Hello, my name is {} and I am {} years old.", self.name, self.age);
        }
    }
    
    fn main() {
        let person = Human {
            name: String::from("Alice"),
            age: 30,
        };
    
        person.greet();
    }
    
  3. Compile and run
    Run:

    cargo run
    

    Expected output:

    Hello, my name is Alice and I am 30 years old.
    

Pitfalls

  • Forgetting to use self in the method definition
  • Using &mut self instead of &self when the method doesn't modify the struct
  • Not implementing the impl block correctly (missing fn keyword)

To Go Further

  • Learn how to implement multiple methods on the same struct
  • Explore how to use self in different ways (&self, &mut self, self)
  • Study how to return values from methods instead of printing them
 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
# How to Add a Greeting Method to the Human Structure

What the reader will be able to do: Define a method on the `Human` struct that prints a personalized greeting.

## Prerequisites
- Basic understanding of Rust structs and mutability (from previous tutorials)
- Familiarity with `fn main()` and `println!`

## Steps

1. **Define the `Human` struct**  
   Open `src/main.rs` and ensure you have:
   ```rust
   struct Human {
       name: String,
       age: u32,
   }
   ```

2. **Add a method to the `Human` struct**  
   Replace the `main()` function with:
   ```rust
   impl Human {
       fn greet(&self) {
           println!("Hello, my name is {} and I am {} years old.", self.name, self.age);
       }
   }

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

       person.greet();
   }
   ```

3. **Compile and run**  
   Run:
   ```bash
   cargo run
   ```
   Expected output:
   ```
   Hello, my name is Alice and I am 30 years old.
   ```

## Pitfalls
- Forgetting to use `self` in the method definition
- Using `&mut self` instead of `&self` when the method doesn't modify the struct
- Not implementing the `impl` block correctly (missing `fn` keyword)

## To Go Further
- Learn how to implement multiple methods on the same struct
- Explore how to use `self` in different ways (`&self`, `&mut self`, `self`)
- Study how to return values from methods instead of printing them