Skip to content

Commit e4d826d

Browse files
committed
Try to elaborate more on what Self defines.
1 parent b494dab commit e4d826d

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/paths.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,19 @@ fn bar() {
206206

207207
### `Self`
208208

209-
`Self`, with a capital "S", is used to refer to the implementing type within
210-
[traits] and [implementations].
209+
`Self`, with a capital "S", is used to refer to the current type being implemented or defined. It may be used in the following situations:
210+
211+
* In a [trait] definition, it refers to the type implementing the trait.
212+
* In an [implementation], it refers to the implementing type.
213+
When implementing a tuple or unit [struct], [enumeration], or [union], it also refers to the constructor in the [value namespace].
214+
* In the definition of a [struct], [enumeration], or [union], it refers to the defining type.
215+
The definition is not allowed to be infinitely recursive (there must be an indirection).
216+
217+
The scope of `Self` behaves similarly to a generic parameter, see the [scopes chapter] for more details.
218+
<!-- TODO: update link to #self-scope once https://github.com/rust-lang/reference/pull/1040 is merged. -->
211219

212220
`Self` can only be used as the first segment, without a preceding `::`.
221+
The `Self` path cannot include generic arguments (as in `Self::<i32>`).
213222

214223
```rust
215224
trait T {
@@ -231,6 +240,20 @@ impl T for S {
231240
Self::C // `Self::C` is the constant value `9`.
232241
}
233242
}
243+
244+
// `Self` is in scope within the generics of a trait definition,
245+
// to refer to the defining type.
246+
trait Add<Rhs = Self> {
247+
type Output;
248+
// `Self` can also reference associated items of the implementing types.
249+
fn add(self, rhs: Rhs) -> Self::Output;
250+
}
251+
252+
struct NonEmptyList<T> {
253+
head: T,
254+
// A struct can reference itself (as long as it is not infinitely recursive).
255+
tail: Option<Box<NonEmptyList<T>>>,
256+
}
234257
```
235258

236259
### `super`
@@ -387,13 +410,20 @@ mod without { // ::without
387410
[IDENTIFIER]: identifiers.md
388411
[`use`]: items/use-declarations.md
389412
[attributes]: attributes.md
413+
[enumeration]: items/enumerations.md
390414
[expressions]: expressions.md
391415
[extern prelude]: names/preludes.md#extern-prelude
416+
[implementation]: items/implementations.md
392417
[macro transcribers]: macros-by-example.md
393418
[macros]: macros.md
394419
[mbe]: macros-by-example.md
395420
[patterns]: patterns.md
421+
[scopes chapter]: names/scopes.md
422+
[struct]: items/structs.md
396423
[trait implementations]: items/implementations.md#trait-implementations
424+
[trait]: items/traits.md
397425
[traits]: items/traits.md
398426
[types]: types.md
427+
[union]: items/unions.md
428+
[value namespace]: names/namespaces.md
399429
[visibility]: visibility-and-privacy.md

0 commit comments

Comments
 (0)