🛟 Updated.
a1a9639 parent: a9e95e5 added
06-struct-method.md +57 -0 | new file mode 100644 | ||
| @@ -0,0 +1,57 @@ | ||
| 1 | +# How to Add a Greeting Method to the Human Structure | |
| 2 | + | |
| 3 | +What the reader will be able to do: Define a method on the `Human` struct that prints a personalized greeting. | |
| 4 | + | |
| 5 | +## Prerequisites | |
| 6 | +- Basic understanding of Rust structs and mutability (from previous tutorials) | |
| 7 | +- Familiarity with `fn main()` and `println!` | |
| 8 | + | |
| 9 | +## Steps | |
| 10 | + | |
| 11 | +1. **Define the `Human` struct** | |
| 12 | + Open `src/main.rs` and ensure you have: | |
| 13 | + ```rust | |
| 14 | + struct Human { | |
| 15 | + name: String, | |
| 16 | + age: u32, | |
| 17 | + } | |
| 18 | + ``` | |
| 19 | + | |
| 20 | +2. **Add a method to the `Human` struct** | |
| 21 | + Replace the `main()` function with: | |
| 22 | + ```rust | |
| 23 | + impl Human { | |
| 24 | + fn greet(&self) { | |
| 25 | + println!("Hello, my name is {} and I am {} years old.", self.name, self.age); | |
| 26 | + } | |
| 27 | + } | |
| 28 | + | |
| 29 | + fn main() { | |
| 30 | + let person = Human { | |
| 31 | + name: String::from("Alice"), | |
| 32 | + age: 30, | |
| 33 | + }; | |
| 34 | + | |
| 35 | + person.greet(); | |
| 36 | + } | |
| 37 | + ``` | |
| 38 | + | |
| 39 | +3. **Compile and run** | |
| 40 | + Run: | |
| 41 | + ```bash | |
| 42 | + cargo run | |
| 43 | + ``` | |
| 44 | + Expected output: | |
| 45 | + ``` | |
| 46 | + Hello, my name is Alice and I am 30 years old. | |
| 47 | + ``` | |
| 48 | + | |
| 49 | +## Pitfalls | |
| 50 | +- Forgetting to use `self` in the method definition | |
| 51 | +- Using `&mut self` instead of `&self` when the method doesn't modify the struct | |
| 52 | +- Not implementing the `impl` block correctly (missing `fn` keyword) | |
| 53 | + | |
| 54 | +## To Go Further | |
| 55 | +- Learn how to implement multiple methods on the same struct | |
| 56 | +- Explore how to use `self` in different ways (`&self`, `&mut self`, `self`) | |
| 57 | +- Study how to return values from methods instead of printing them | |
| new file mode 100644 | |||
| @@ -0,0 +1,57 @@ | |||
| 1 | +# How to Add a Greeting Method to the Human Structure | ||
| 2 | + | ||
| 3 | +What the reader will be able to do: Define a method on the `Human` struct that prints a personalized greeting. | ||
| 4 | + | ||
| 5 | +## Prerequisites | ||
| 6 | +- Basic understanding of Rust structs and mutability (from previous tutorials) | ||
| 7 | +- Familiarity with `fn main()` and `println!` | ||
| 8 | + | ||
| 9 | +## Steps | ||
| 10 | + | ||
| 11 | +1. **Define the `Human` struct** | ||
| 12 | + Open `src/main.rs` and ensure you have: | ||
| 13 | + ```rust | ||
| 14 | + struct Human { | ||
| 15 | + name: String, | ||
| 16 | + age: u32, | ||
| 17 | + } | ||
| 18 | + ``` | ||
| 19 | + | ||
| 20 | +2. **Add a method to the `Human` struct** | ||
| 21 | + Replace the `main()` function with: | ||
| 22 | + ```rust | ||
| 23 | + impl Human { | ||
| 24 | + fn greet(&self) { | ||
| 25 | + println!("Hello, my name is {} and I am {} years old.", self.name, self.age); | ||
| 26 | + } | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + fn main() { | ||
| 30 | + let person = Human { | ||
| 31 | + name: String::from("Alice"), | ||
| 32 | + age: 30, | ||
| 33 | + }; | ||
| 34 | + | ||
| 35 | + person.greet(); | ||
| 36 | + } | ||
| 37 | + ``` | ||
| 38 | + | ||
| 39 | +3. **Compile and run** | ||
| 40 | + Run: | ||
| 41 | + ```bash | ||
| 42 | + cargo run | ||
| 43 | + ``` | ||
| 44 | + Expected output: | ||
| 45 | + ``` | ||
| 46 | + Hello, my name is Alice and I am 30 years old. | ||
| 47 | + ``` | ||
| 48 | + | ||
| 49 | +## Pitfalls | ||
| 50 | +- Forgetting to use `self` in the method definition | ||
| 51 | +- Using `&mut self` instead of `&self` when the method doesn't modify the struct | ||
| 52 | +- Not implementing the `impl` block correctly (missing `fn` keyword) | ||
| 53 | + | ||
| 54 | +## To Go Further | ||
| 55 | +- Learn how to implement multiple methods on the same struct | ||
| 56 | +- Explore how to use `self` in different ways (`&self`, `&mut self`, `self`) | ||
| 57 | +- Study how to return values from methods instead of printing them | ||
modified
hello_rust/src/main.rs +16 -6 | @@ -3,6 +3,15 @@ struct Human { | ||
| 3 | 3 | age: u32, |
| 4 | 4 | } |
| 5 | 5 | |
| 6 | +impl Human { | |
| 7 | + fn greet(&self) { | |
| 8 | + println!( | |
| 9 | + "Hello, my name is {} and I am {} years old.", | |
| 10 | + self.name, self.age | |
| 11 | + ); | |
| 12 | + } | |
| 13 | +} | |
| 14 | + | |
| 6 | 15 | fn main() { |
| 7 | 16 | println!("Hello, world!"); |
| 8 | 17 | |
| @@ -11,14 +20,15 @@ fn main() { | ||
| 11 | 20 | age: 30, |
| 12 | 21 | }; |
| 13 | 22 | |
| 14 | - println!("Name: {}", person.name); | |
| 15 | - println!("Age: {}", person.age); | |
| 16 | - | |
| 23 | + // println!("Name: {}", person.name); | |
| 24 | + // println!("Age: {}", person.age); | |
| 25 | + person.greet(); | |
| 26 | + | |
| 17 | 27 | person.name = String::from("Bob"); |
| 18 | 28 | person.age = 42; |
| 19 | 29 | |
| 20 | 30 | println!("Some changes:"); |
| 21 | - println!("Name: {}", person.name); | |
| 22 | - println!("Age: {}", person.age); | |
| 23 | - | |
| 31 | + // println!("Name: {}", person.name); | |
| 32 | + // println!("Age: {}", person.age); | |
| 33 | + person.greet(); | |
| 24 | 34 | } |
| @@ -3,6 +3,15 @@ struct Human { | |||
| 3 | age: u32, | 3 | age: u32, |
| 4 | } | 4 | } |
| 5 | 5 | ||
| 6 | +impl Human { | ||
| 7 | + fn greet(&self) { | ||
| 8 | + println!( | ||
| 9 | + "Hello, my name is {} and I am {} years old.", | ||
| 10 | + self.name, self.age | ||
| 11 | + ); | ||
| 12 | + } | ||
| 13 | +} | ||
| 14 | + | ||
| 6 | fn main() { | 15 | fn main() { |
| 7 | println!("Hello, world!"); | 16 | println!("Hello, world!"); |
| 8 | 17 | ||
| @@ -11,14 +20,15 @@ fn main() { | |||
| 11 | age: 30, | 20 | age: 30, |
| 12 | }; | 21 | }; |
| 13 | 22 | ||
| 14 | - println!("Name: {}", person.name); | 23 | + // println!("Name: {}", person.name); |
| 15 | - println!("Age: {}", person.age); | 24 | + // println!("Age: {}", person.age); |
| 16 | - | 25 | + person.greet(); |
| 26 | + | ||
| 17 | person.name = String::from("Bob"); | 27 | person.name = String::from("Bob"); |
| 18 | person.age = 42; | 28 | person.age = 42; |
| 19 | 29 | ||
| 20 | println!("Some changes:"); | 30 | println!("Some changes:"); |
| 21 | - println!("Name: {}", person.name); | 31 | + // println!("Name: {}", person.name); |
| 22 | - println!("Age: {}", person.age); | 32 | + // println!("Age: {}", person.age); |
| 23 | - | 33 | + person.greet(); |
| 24 | } | 34 | } |