Skip to content

Commit

Permalink
Rollup merge of rust-lang#73694 - poliorcetics:self-upper-keyword, r=…
Browse files Browse the repository at this point in the history
…Mark-Simulacrum

Document the Self keyword

Partial fix of rust-lang#34601.

Document the `Self` keyword.

This contains simple examples of the places where `Self` can be used.
  • Loading branch information
Manishearth committed Jun 26, 2020
2 parents ce114af + f44b8b9 commit d8929b5
Showing 1 changed file with 57 additions and 2 deletions.
59 changes: 57 additions & 2 deletions src/libstd/keyword_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down

0 comments on commit d8929b5

Please sign in to comment.