diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2020-06-26 13:57:39 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-26 13:57:39 -0700 |
| commit | 8d9c00d7f1b00113d1052d1e44948ec6e8597a76 (patch) | |
| tree | cbd7577dc2294bd04d74dba5a38a3ccf8fac0ed4 | |
| parent | c18e919a79c701d25b33cffa0a183ee21085f2e6 (diff) | |
| parent | f44b8b96aa30d03c8e4de56a13f656a422910dec (diff) | |
| download | rust-8d9c00d7f1b00113d1052d1e44948ec6e8597a76.tar.gz rust-8d9c00d7f1b00113d1052d1e44948ec6e8597a76.zip | |
Rollup merge of #73694 - poliorcetics:self-upper-keyword, r=Mark-Simulacrum
Document the Self keyword Partial fix of #34601. Document the `Self` keyword. This contains simple examples of the places where `Self` can be used.
| -rw-r--r-- | src/libstd/keyword_docs.rs | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/src/libstd/keyword_docs.rs b/src/libstd/keyword_docs.rs index f987eb67ea5..a4c48b696dd 100644 --- a/src/libstd/keyword_docs.rs +++ b/src/libstd/keyword_docs.rs @@ -1217,11 +1217,66 @@ mod self_keyword {} /// The implementing type within a [`trait`] or [`impl`] block, or the current type within a type /// definition. /// -/// The documentation for this keyword is [not yet complete]. Pull requests welcome! +/// Within a type definition: +/// +/// ``` +/// # #![allow(dead_code)] +/// struct Node { +/// elem: i32, +/// // `Self` is a `Node` here. +/// next: Option<Box<Self>>, +/// } +/// ``` +/// +/// In an [`impl`] block: +/// +/// ``` +/// struct Foo(i32); +/// +/// impl Foo { +/// fn new() -> Self { +/// Self(0) +/// } +/// } +/// +/// assert_eq!(Foo::new().0, Foo(0).0); +/// ``` +/// +/// Generic parameters are implicit with `Self`: +/// +/// ``` +/// # #![allow(dead_code)] +/// struct Wrap<T> { +/// elem: T, +/// } +/// +/// impl<T> Wrap<T> { +/// fn new(elem: T) -> Self { +/// Self { elem } +/// } +/// } +/// ``` +/// +/// In a [`trait`] definition and related [`impl`] block: +/// +/// ``` +/// trait Example { +/// fn example() -> Self; +/// } +/// +/// struct Foo(i32); +/// +/// impl Example for Foo { +/// fn example() -> Self { +/// Self(42) +/// } +/// } +/// +/// assert_eq!(Foo::example().0, Foo(42).0); +/// ``` /// /// [`impl`]: keyword.impl.html /// [`trait`]: keyword.trait.html -/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601 mod self_upper_keyword {} #[doc(keyword = "static")] |
