Merge pull request #3857 from polazarus/patch-1

[rust/en] Change misleading method and add two other methods
This commit is contained in:
Divay Prakash 2020-02-16 19:29:13 +05:30 committed by GitHub
commit 9641d773d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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) //