mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-25 10:25:23 +00:00
Merge pull request #3857 from polazarus/patch-1
[rust/en] Change misleading method and add two other methods
This commit is contained in:
commit
9641d773d1
@ -176,13 +176,19 @@ fn main() {
|
|||||||
|
|
||||||
impl<T> Foo<T> {
|
impl<T> Foo<T> {
|
||||||
// Methods take an explicit `self` parameter
|
// Methods take an explicit `self` parameter
|
||||||
fn get_bar(self) -> T {
|
fn bar(&self) -> &T { // self is borrowed
|
||||||
|
&self.bar
|
||||||
|
}
|
||||||
|
fn bar_mut(&mut self) -> &mut T { // self is mutably borrowed
|
||||||
|
&mut self.bar
|
||||||
|
}
|
||||||
|
fn into_bar(self) -> T { // here self is consumed
|
||||||
self.bar
|
self.bar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let a_foo = Foo { bar: 1 };
|
let a_foo = Foo { bar: 1 };
|
||||||
println!("{}", a_foo.get_bar()); // 1
|
println!("{}", a_foo.bar()); // 1
|
||||||
|
|
||||||
// Traits (known as interfaces or typeclasses in other languages) //
|
// Traits (known as interfaces or typeclasses in other languages) //
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user