@@ -1009,9 +1009,93 @@ mod return_keyword {}
10091009//
10101010/// The receiver of a method, or the current module.
10111011///
1012- /// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1012+ /// `self` is used in two situations: referencing the current module and marking
1013+ /// the receiver of a method.
10131014///
1014- /// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
1015+ /// In paths, `self` can be used to refer to the current module, either in a
1016+ /// [`use`] statement or in a path to access an element:
1017+ ///
1018+ /// ```
1019+ /// # #![allow(unused_imports)]
1020+ /// use std::io::{self, Read};
1021+ /// ```
1022+ ///
1023+ /// Is functionally the same as:
1024+ ///
1025+ /// ```
1026+ /// # #![allow(unused_imports)]
1027+ /// use std::io;
1028+ /// use std::io::Read;
1029+ /// ```
1030+ ///
1031+ /// Using `self` to access an element in the current module:
1032+ ///
1033+ /// ```
1034+ /// # #![allow(dead_code)]
1035+ /// # fn main() {}
1036+ /// fn foo() {}
1037+ /// fn bar() {
1038+ /// self::foo()
1039+ /// }
1040+ /// ```
1041+ ///
1042+ /// `self` as the current receiver for a method allows to omit the parameter
1043+ /// type most of the time. With the exception of this particularity, `self` is
1044+ /// used much like any other parameter:
1045+ ///
1046+ /// ```
1047+ /// struct Foo(i32);
1048+ ///
1049+ /// impl Foo {
1050+ /// // No `self`.
1051+ /// fn new() -> Self {
1052+ /// Self(0)
1053+ /// }
1054+ ///
1055+ /// // Consuming `self`.
1056+ /// fn consume(self) -> Self {
1057+ /// Self(self.0 + 1)
1058+ /// }
1059+ ///
1060+ /// // Borrowing `self`.
1061+ /// fn borrow(&self) -> &i32 {
1062+ /// &self.0
1063+ /// }
1064+ ///
1065+ /// // Borrowing `self` mutably.
1066+ /// fn borrow_mut(&mut self) -> &mut i32 {
1067+ /// &mut self.0
1068+ /// }
1069+ /// }
1070+ ///
1071+ /// // This method must be called with a `Type::` prefix.
1072+ /// let foo = Foo::new();
1073+ /// assert_eq!(foo.0, 0);
1074+ ///
1075+ /// // Those two calls produces the same result.
1076+ /// let foo = Foo::consume(foo);
1077+ /// assert_eq!(foo.0, 1);
1078+ /// let foo = foo.consume();
1079+ /// assert_eq!(foo.0, 2);
1080+ ///
1081+ /// // Borrowing is handled automatically with the second syntax.
1082+ /// let borrow_1 = Foo::borrow(&foo);
1083+ /// let borrow_2 = foo.borrow();
1084+ /// assert_eq!(borrow_1, borrow_2);
1085+ ///
1086+ /// // Borrowing mutably is handled automatically too with the second syntax.
1087+ /// let mut foo = Foo::new();
1088+ /// *Foo::borrow_mut(&mut foo) += 1;
1089+ /// assert_eq!(foo.0, 1);
1090+ /// *foo.borrow_mut() += 1;
1091+ /// assert_eq!(foo.0, 2);
1092+ /// ```
1093+ ///
1094+ /// Note that this automatic conversion when calling `foo.method()` is not
1095+ /// limited to the examples above. See the [Reference] for more information.
1096+ ///
1097+ /// [`use`]: keyword.use.html
1098+ /// [Reference]: ../reference/items/associated-items.html#methods
10151099mod self_keyword { }
10161100
10171101#[ doc( keyword = "Self" ) ]
0 commit comments