-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0c77824
commit e1d31b3
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
### Is **self** a special keywork in Rust? | ||
|
||
**self** is a special keyword in Rust. It is used to refer to the instance of the struct or enum within its associated methods. It is similar to this in other object-oriented languages like Java or C++. | ||
|
||
### Usage of self | ||
|
||
**Method Definitions**: When defining methods for a struct or enum, self is used to refer to the instance on which the method is called. | ||
|
||
**Method Parameters**: self can be used as a parameter in method definitions to indicate that the method takes ownership, borrows immutably, or borrows mutably from the instance. | ||
|
||
### Immutable Borrow (&self) | ||
|
||
When a method takes &self as a parameter, it means the method borrows the instance immutably. | ||
|
||
```rust | ||
pub struct Person { | ||
name: String, | ||
} | ||
|
||
impl Person { | ||
// Method that borrows the instance immutably | ||
fn greet(&self) { | ||
println!("Hello, my name is {}!", self.name); | ||
} | ||
} | ||
|
||
fn main() { | ||
let person = Person { | ||
name: String::from("Alice"), | ||
}; | ||
person.greet(); // Calls the greet method | ||
} | ||
``` | ||
|
||
### Mutable Borrow (&mut self) | ||
|
||
When a method takes &mut self as a parameter, it means the method borrows the instance mutably. | ||
|
||
```rust | ||
pub struct Counter { | ||
count: i32, | ||
} | ||
|
||
impl Counter { | ||
// Method that borrows the instance mutably | ||
fn increment(&mut self) { | ||
self.count += 1; | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut counter = Counter { count: 0 }; | ||
counter.increment(); // Calls the increment method | ||
println!("Count: {}", counter.count); | ||
counter.increment(); // Calls the increment method | ||
println!("Count: {}", counter.count); | ||
} | ||
``` | ||
|
||
### Ownership (self) | ||
|
||
When a method takes self as a parameter, it means the method takes ownership of the instance. | ||
|
||
```rust | ||
pub struct Person { | ||
name: String, | ||
} | ||
|
||
impl Person { | ||
// Method that takes ownership of the instance | ||
fn into_name(self) -> String { | ||
self.name | ||
} | ||
} | ||
|
||
fn main() { | ||
let person = Person { | ||
name: String::from("Alice"), | ||
}; | ||
let name = person.into_name(); // Calls the into_name method, taking ownership | ||
println!("Name: {}", name); | ||
} | ||
``` |