Skip to content

Commit f44b8b9

Browse files
committed
Document the Self keyword
1 parent 3c90ae8 commit f44b8b9

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

src/libstd/keyword_docs.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -1019,11 +1019,66 @@ mod self_keyword {}
10191019
/// The implementing type within a [`trait`] or [`impl`] block, or the current type within a type
10201020
/// definition.
10211021
///
1022-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1022+
/// Within a type definition:
1023+
///
1024+
/// ```
1025+
/// # #![allow(dead_code)]
1026+
/// struct Node {
1027+
/// elem: i32,
1028+
/// // `Self` is a `Node` here.
1029+
/// next: Option<Box<Self>>,
1030+
/// }
1031+
/// ```
1032+
///
1033+
/// In an [`impl`] block:
1034+
///
1035+
/// ```
1036+
/// struct Foo(i32);
1037+
///
1038+
/// impl Foo {
1039+
/// fn new() -> Self {
1040+
/// Self(0)
1041+
/// }
1042+
/// }
1043+
///
1044+
/// assert_eq!(Foo::new().0, Foo(0).0);
1045+
/// ```
1046+
///
1047+
/// Generic parameters are implicit with `Self`:
1048+
///
1049+
/// ```
1050+
/// # #![allow(dead_code)]
1051+
/// struct Wrap<T> {
1052+
/// elem: T,
1053+
/// }
1054+
///
1055+
/// impl<T> Wrap<T> {
1056+
/// fn new(elem: T) -> Self {
1057+
/// Self { elem }
1058+
/// }
1059+
/// }
1060+
/// ```
1061+
///
1062+
/// In a [`trait`] definition and related [`impl`] block:
1063+
///
1064+
/// ```
1065+
/// trait Example {
1066+
/// fn example() -> Self;
1067+
/// }
1068+
///
1069+
/// struct Foo(i32);
1070+
///
1071+
/// impl Example for Foo {
1072+
/// fn example() -> Self {
1073+
/// Self(42)
1074+
/// }
1075+
/// }
1076+
///
1077+
/// assert_eq!(Foo::example().0, Foo(42).0);
1078+
/// ```
10231079
///
10241080
/// [`impl`]: keyword.impl.html
10251081
/// [`trait`]: keyword.trait.html
1026-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
10271082
mod self_upper_keyword {}
10281083

10291084
#[doc(keyword = "static")]

0 commit comments

Comments
 (0)