Skip to content

Commit 6fe62a5

Browse files
committed
Document '_
1 parent d9e6a59 commit 6fe62a5

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/lifetime-elision.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ compiler can infer a sensible default choice.
77

88
In order to make common patterns more ergonomic, Rust allows lifetimes to be
99
*elided* in [function item], [function pointer] and [closure trait] signatures.
10-
The following rules are used to infer lifetime parameters for elided lifetimes.
11-
It is an error to elide lifetime parameters that cannot be inferred.
10+
An elided lifetime can be specified either by omitting it, or by using `'_`.
11+
For lifetimes in paths, using `'_` is preferred. The following rules are used
12+
to infer lifetime parameters for elided lifetimes. It is an error to elide
13+
lifetime parameters that cannot be inferred.
1214

1315
* Each elided lifetime in the parameters becomes a distinct lifetime parameter.
1416
* If there is exactly one lifetime used in the parameters (elided or not), that
@@ -23,6 +25,7 @@ Examples:
2325

2426
```rust,ignore
2527
fn print(s: &str); // elided
28+
fn print(s: &'_ str); // also elided
2629
fn print<'a>(s: &'a str); // expanded
2730
2831
fn debug(lvl: usize, s: &str); // elided
@@ -41,6 +44,7 @@ fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
4144
fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command; // elided
4245
fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command; // expanded
4346
47+
fn new(buf: &mut [u8]) -> BufWriter<'_>; // elided - preferred
4448
fn new(buf: &mut [u8]) -> BufWriter; // elided
4549
fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a>; // expanded
4650
@@ -55,8 +59,12 @@ type FunTrait = for<'a> Fn(&'a str) -> &'a str; // expanded
5559

5660
The assumed lifetime of references held by a [trait object] is called its
5761
_default object lifetime bound_. These were defined in [RFC 599] and amended in
58-
[RFC 1156]. Default object lifetime bounds are used instead of the lifetime
59-
parameter elision rules defined above.
62+
[RFC 1156].
63+
64+
> These default object lifetime bounds are used instead of the lifetime
65+
> parameter elision rules defined above when the lifetime bound is omitted
66+
> entirely. If `'_` is used as the lifetime bound then the bound follows the
67+
> usual elision rules.
6068
6169
If the trait object is used as a type argument of a generic type then the
6270
containing type is first used to try to infer a bound.
@@ -136,7 +144,7 @@ struct BitsNStrings<'a> {
136144
}
137145

138146
// BITS_N_STRINGS: BitsNStrings<'static>
139-
const BITS_N_STRINGS: BitsNStrings = BitsNStrings {
147+
const BITS_N_STRINGS: BitsNStrings<'_> = BitsNStrings {
140148
mybits: [1, 2],
141149
mystring: STRING,
142150
};

src/trait-bounds.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
> _Lifetime_ :
2020
> &nbsp;&nbsp; &nbsp;&nbsp; [LIFETIME_OR_LABEL]
2121
> &nbsp;&nbsp; | `'static`
22+
> &nbsp;&nbsp; | `'_`
2223
2324
[Trait] and lifetime bounds provide a way for [generic items][generic] to
2425
restrict which types and lifetimes are used as their parameters. Bounds can be

0 commit comments

Comments
 (0)