Skip to content

Commit 6fbd064

Browse files
committed
Add a dedicated chapter for generic lifetimes
This is only a bare-bones skeleton. The intent is that this should get filled out with more rules specific to generic lifetimes.
1 parent 68550ec commit 6fbd064

4 files changed

Lines changed: 32 additions & 1 deletion

File tree

book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ use-boolean-and = true
5151
"/glossary.html#object-safe-traits" = "glossary.html#dyn-compatible-traits"
5252
"/items/extern-crates.html#extern-prelude" = "../names/preludes.html#extern-prelude"
5353
"/items/generics.html" = "../types/generics/index.html"
54+
"/items/generics.html#generic-lifetimes" = "../types/generics/lifetimes.html"
5455
"/items/generics.html#where-clauses" = "../trait-bounds.html#where-clauses"
5556
"/items/modules.html#prelude-items" = "../names/preludes.html"
5657
"/items/traits.html#object-safety" = "traits.html#dyn-compatibility"

src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
- [Type parameters](types/parameters.md)
9494
- [Inferred type](types/inferred.md)
9595
- [Generics](types/generics/index.md)
96+
- [Generic lifetimes](types/generics/lifetimes.md)
9697
- [Generic constants](types/generics/constants.md)
9798
- [Dynamically sized types](dynamically-sized-types.md)
9899
- [Type layout](type-layout.md)

src/types/generics/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ r[generics]
22
# Generics
33

44
r[generics.intro]
5-
Generics allow items to be parameterized by types, lifetimes, and [constants]. This allows definitions to be written in a flexible way that can be reused with different concrete types and values.
5+
Generics allow items to be parameterized by types, [lifetimes], and [constants]. This allows definitions to be written in a flexible way that can be reused with different concrete types and values.
66

77
r[generics.parameters]
88
## Generic parameters
@@ -352,6 +352,7 @@ The [built-in attributes] that have meaning on a generic parameter are [`cfg`] a
352352
[implementations]: items.impl
353353
[item declarations]: statement.item
354354
[lifetime elision]: lifetime-elision
355+
[lifetimes]: generics.lifetimes
355356
[paths in expressions]: paths.expr
356357
[paths in types]: paths.type
357358
[qualified path]: paths.qualified

src/types/generics/lifetimes.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
r[generics.lifetimes]
2+
# Generic lifetimes
3+
4+
r[generics.lifetimes.intro]
5+
A *lifetime parameter* is a generic parameter for a lifetime.
6+
7+
r[generics.lifetimes.at-least-once]
8+
[Structs], [enumerations], and [unions] must use each of their lifetime parameters at least once in their fields or variants.
9+
10+
> [!EXAMPLE]
11+
> ```rust,compile_fail
12+
> // ERROR: lifetime parameter `'a` is never used
13+
> struct Foo<'a>;
14+
>
15+
> // ERROR: lifetime parameter `'a` is never used
16+
> enum Bar<'a> { A }
17+
> ```
18+
>
19+
> ```rust
20+
> // OK: `'a` appears in a field.
21+
> struct Ref<'a, T> {
22+
> r: &'a T,
23+
> }
24+
> ```
25+
26+
[enumerations]: items.enum
27+
[structs]: items.struct
28+
[unions]: items.union

0 commit comments

Comments
 (0)