Skip to content

Commit 528b1a2

Browse files
committed
Try to elaborate more on what Self defines.
1 parent 5de59db commit 528b1a2

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
@@ -221,10 +221,19 @@ impl S {
221221

222222
### `Self`
223223

224-
`Self`, with a capital "S", is used to refer to the implementing type within
225-
[traits] and [implementations].
224+
`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:
225+
226+
* In a [trait] definition, it refers to the type implementing the trait.
227+
* In an [implementation], it refers to the implementing type.
228+
When implementing a tuple or unit [struct], [enumeration], or [union], it also refers to the constructor in the [value namespace].
229+
* In the definition of a [struct], [enumeration], or [union], it refers to the defining type.
230+
The definition is not allowed to be infinitely recursive (there must be an indirection).
231+
232+
The scope of `Self` behaves similarly to a generic parameter, see the [scopes chapter] for more details.
233+
<!-- TODO: update link to #self-scope once https://github.com/rust-lang/reference/pull/1040 is merged. -->
226234

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

229238
```rust
230239
trait T {
@@ -246,6 +255,20 @@ impl T for S {
246255
Self::C // `Self::C` is the constant value `9`.
247256
}
248257
}
258+
259+
// `Self` is in scope within the generics of a trait definition,
260+
// to refer to the defining type.
261+
trait Add<Rhs = Self> {
262+
type Output;
263+
// `Self` can also reference associated items of the implementing types.
264+
fn add(self, rhs: Rhs) -> Self::Output;
265+
}
266+
267+
struct NonEmptyList<T> {
268+
head: T,
269+
// A struct can reference itself (as long as it is not infinitely recursive).
270+
tail: Option<Box<NonEmptyList<T>>>,
271+
}
249272
```
250273

251274
### `super`
@@ -404,13 +427,20 @@ mod without { // crate::without
404427
[IDENTIFIER]: identifiers.md
405428
[`use`]: items/use-declarations.md
406429
[attributes]: attributes.md
430+
[enumeration]: items/enumerations.md
407431
[expressions]: expressions.md
408432
[extern prelude]: names/preludes.md#extern-prelude
433+
[implementation]: items/implementations.md
409434
[macro transcribers]: macros-by-example.md
410435
[macros]: macros.md
411436
[mbe]: macros-by-example.md
412437
[patterns]: patterns.md
438+
[scopes chapter]: names/scopes.md
439+
[struct]: items/structs.md
413440
[trait implementations]: items/implementations.md#trait-implementations
441+
[trait]: items/traits.md
414442
[traits]: items/traits.md
415443
[types]: types.md
444+
[union]: items/unions.md
445+
[value namespace]: names/namespaces.md
416446
[visibility]: visibility-and-privacy.md

0 commit comments

Comments
 (0)