Rollup merge of #73688 - poliorcetics:self-keyword, r=joshtriplett
Document the self keyword Partial fix of #34601. This documents the `self` keyword, adding several examples and a link to the reference.
This commit is contained in:
commit
a5839f35b6
1 changed files with 86 additions and 2 deletions
|
@ -1009,9 +1009,93 @@ mod return_keyword {}
|
|||
//
|
||||
/// The receiver of a method, or the current module.
|
||||
///
|
||||
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
|
||||
/// `self` is used in two situations: referencing the current module and marking
|
||||
/// the receiver of a method.
|
||||
///
|
||||
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
|
||||
/// In paths, `self` can be used to refer to the current module, either in a
|
||||
/// [`use`] statement or in a path to access an element:
|
||||
///
|
||||
/// ```
|
||||
/// # #![allow(unused_imports)]
|
||||
/// use std::io::{self, Read};
|
||||
/// ```
|
||||
///
|
||||
/// Is functionally the same as:
|
||||
///
|
||||
/// ```
|
||||
/// # #![allow(unused_imports)]
|
||||
/// use std::io;
|
||||
/// use std::io::Read;
|
||||
/// ```
|
||||
///
|
||||
/// Using `self` to access an element in the current module:
|
||||
///
|
||||
/// ```
|
||||
/// # #![allow(dead_code)]
|
||||
/// # fn main() {}
|
||||
/// fn foo() {}
|
||||
/// fn bar() {
|
||||
/// self::foo()
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// `self` as the current receiver for a method allows to omit the parameter
|
||||
/// type most of the time. With the exception of this particularity, `self` is
|
||||
/// used much like any other parameter:
|
||||
///
|
||||
/// ```
|
||||
/// struct Foo(i32);
|
||||
///
|
||||
/// impl Foo {
|
||||
/// // No `self`.
|
||||
/// fn new() -> Self {
|
||||
/// Self(0)
|
||||
/// }
|
||||
///
|
||||
/// // Consuming `self`.
|
||||
/// fn consume(self) -> Self {
|
||||
/// Self(self.0 + 1)
|
||||
/// }
|
||||
///
|
||||
/// // Borrowing `self`.
|
||||
/// fn borrow(&self) -> &i32 {
|
||||
/// &self.0
|
||||
/// }
|
||||
///
|
||||
/// // Borrowing `self` mutably.
|
||||
/// fn borrow_mut(&mut self) -> &mut i32 {
|
||||
/// &mut self.0
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // This method must be called with a `Type::` prefix.
|
||||
/// let foo = Foo::new();
|
||||
/// assert_eq!(foo.0, 0);
|
||||
///
|
||||
/// // Those two calls produces the same result.
|
||||
/// let foo = Foo::consume(foo);
|
||||
/// assert_eq!(foo.0, 1);
|
||||
/// let foo = foo.consume();
|
||||
/// assert_eq!(foo.0, 2);
|
||||
///
|
||||
/// // Borrowing is handled automatically with the second syntax.
|
||||
/// let borrow_1 = Foo::borrow(&foo);
|
||||
/// let borrow_2 = foo.borrow();
|
||||
/// assert_eq!(borrow_1, borrow_2);
|
||||
///
|
||||
/// // Borrowing mutably is handled automatically too with the second syntax.
|
||||
/// let mut foo = Foo::new();
|
||||
/// *Foo::borrow_mut(&mut foo) += 1;
|
||||
/// assert_eq!(foo.0, 1);
|
||||
/// *foo.borrow_mut() += 1;
|
||||
/// assert_eq!(foo.0, 2);
|
||||
/// ```
|
||||
///
|
||||
/// Note that this automatic conversion when calling `foo.method()` is not
|
||||
/// limited to the examples above. See the [Reference] for more information.
|
||||
///
|
||||
/// [`use`]: keyword.use.html
|
||||
/// [Reference]: ../reference/items/associated-items.html#methods
|
||||
mod self_keyword {}
|
||||
|
||||
#[doc(keyword = "Self")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue