Skip to content

Commit f52542a

Browse files
authored
Rollup merge of rust-lang#73694 - poliorcetics:self-upper-keyword, r=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.
2 parents eff76f3 + f44b8b9 commit f52542a

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

Diff for: src/libstd/keyword_docs.rs

+57-2
Original file line numberDiff line numberDiff line change
@@ -1217,11 +1217,66 @@ mod self_keyword {}
12171217
/// The implementing type within a [`trait`] or [`impl`] block, or the current type within a type
12181218
/// definition.
12191219
///
1220-
/// The documentation for this keyword is [not yet complete]. Pull requests welcome!
1220+
/// Within a type definition:
1221+
///
1222+
/// ```
1223+
/// # #![allow(dead_code)]
1224+
/// struct Node {
1225+
/// elem: i32,
1226+
/// // `Self` is a `Node` here.
1227+
/// next: Option<Box<Self>>,
1228+
/// }
1229+
/// ```
1230+
///
1231+
/// In an [`impl`] block:
1232+
///
1233+
/// ```
1234+
/// struct Foo(i32);
1235+
///
1236+
/// impl Foo {
1237+
/// fn new() -> Self {
1238+
/// Self(0)
1239+
/// }
1240+
/// }
1241+
///
1242+
/// assert_eq!(Foo::new().0, Foo(0).0);
1243+
/// ```
1244+
///
1245+
/// Generic parameters are implicit with `Self`:
1246+
///
1247+
/// ```
1248+
/// # #![allow(dead_code)]
1249+
/// struct Wrap<T> {
1250+
/// elem: T,
1251+
/// }
1252+
///
1253+
/// impl<T> Wrap<T> {
1254+
/// fn new(elem: T) -> Self {
1255+
/// Self { elem }
1256+
/// }
1257+
/// }
1258+
/// ```
1259+
///
1260+
/// In a [`trait`] definition and related [`impl`] block:
1261+
///
1262+
/// ```
1263+
/// trait Example {
1264+
/// fn example() -> Self;
1265+
/// }
1266+
///
1267+
/// struct Foo(i32);
1268+
///
1269+
/// impl Example for Foo {
1270+
/// fn example() -> Self {
1271+
/// Self(42)
1272+
/// }
1273+
/// }
1274+
///
1275+
/// assert_eq!(Foo::example().0, Foo(42).0);
1276+
/// ```
12211277
///
12221278
/// [`impl`]: keyword.impl.html
12231279
/// [`trait`]: keyword.trait.html
1224-
/// [not yet complete]: https://github.com/rust-lang/rust/issues/34601
12251280
mod self_upper_keyword {}
12261281

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

0 commit comments

Comments
 (0)