Skip to content

Commit

Permalink
Rollup merge of #135604 - estebank:docs-e0207, r=jieyouxu
Browse files Browse the repository at this point in the history
Expand docs for `E0207` with additional example

Add an example to E0207 docs showing how to tie the lifetime of the self type to an associated type in an impl when the trait *doesn't* have a lifetime to begin with.

CC #135589.
  • Loading branch information
matthiaskrgr authored Jan 17, 2025
2 parents a5ca2a0 + 9bdc658 commit 479e0c1
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0207.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ impl<'a> Contains for Foo {
Please note that unconstrained lifetime parameters are not supported if they are
being used by an associated type.

In cases where the associated type's lifetime is meant to be tied to the the
self type, and none of the methods on the trait need ownership or different
mutability, then an option is to implement the trait on a borrowed type:

```rust
struct Foo(i32);

trait Contents {
type Item;

fn get(&self) -> Self::Item;
}

// Note the lifetime `'a` is used both for the self type...
impl<'a> Contents for &'a Foo {
// ...and the associated type.
type Item = &'a i32;

fn get(&self) -> Self::Item {
&self.0
}
}
```

### Additional information

For more information, please see [RFC 447].
Expand Down

0 comments on commit 479e0c1

Please sign in to comment.