Skip to content

Commit

Permalink
add anonymous lifetime
Browse files Browse the repository at this point in the history
Fixes #1273
  • Loading branch information
steveklabnik committed Jul 16, 2018
1 parent a3d38d6 commit 6f1bafe
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 2018-edition/src/ch19-02-advanced-lifetimes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ look at three advanced features of lifetimes that we haven’t covered yet:
* Lifetime bounds: specifies a lifetime for a reference to a generic type
* Inference of trait object lifetimes: allows the compiler to infer trait
object lifetimes and when they need to be specified
* The anonymous lifetime: making elision more obvious

### Ensuring One Lifetime Outlives Another with Lifetime Subtyping

Expand Down Expand Up @@ -440,4 +441,45 @@ As with the other bounds, the syntax adding a lifetime bound means that any
implementor of the `Red` trait that has references inside the type must have
the same lifetime specified in the trait object bounds as those references.

### The anonymous lifetime

Let's say that we have a struct that's a wrapper around a string slice, like
this:

```rust
struct StrWrap<'a>(&'a str);
```

We can write a function that returns one of these like this:

```rust
fn foo<'a>(string: &'a str) -> StrWrap<'a> {
StrWrap(string)
}
```

But that's a lot of `'a`s! To cut down on some of this noise, we can use the
anonymous lifetime, `'_`, like this:

```rust
fn foo(string: &str) -> StrWrap<'_> {
StrWrap(string)
}
```

The `'_` says "use the elidied lifetime here." This means that we can still see
that `StrWrap` contains a reference, but we don't need all of the lifetime
annotations to make sense of it.

It works in `impl` headers too; for example:

```rust,ignore
// verbose
impl<'a> fmt::Debug for StrWrap<'a> {
// elided
impl fmt::Debug for StrWrap<'_> {
```

Next, let’s look at some other advanced features that manage traits.

0 comments on commit 6f1bafe

Please sign in to comment.