Skip to content

Commit 1360e76

Browse files
authoredJan 17, 2025··
Rollup merge of #135604 - estebank:docs-e0207, r=jieyouxu
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.
2 parents 8280407 + 9bdc658 commit 1360e76

File tree

1 file changed

+24
-0
lines changed
  • compiler/rustc_error_codes/src/error_codes

1 file changed

+24
-0
lines changed
 

‎compiler/rustc_error_codes/src/error_codes/E0207.md

+24
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ impl<'a> Contains for Foo {
195195
Please note that unconstrained lifetime parameters are not supported if they are
196196
being used by an associated type.
197197

198+
In cases where the associated type's lifetime is meant to be tied to the the
199+
self type, and none of the methods on the trait need ownership or different
200+
mutability, then an option is to implement the trait on a borrowed type:
201+
202+
```rust
203+
struct Foo(i32);
204+
205+
trait Contents {
206+
type Item;
207+
208+
fn get(&self) -> Self::Item;
209+
}
210+
211+
// Note the lifetime `'a` is used both for the self type...
212+
impl<'a> Contents for &'a Foo {
213+
// ...and the associated type.
214+
type Item = &'a i32;
215+
216+
fn get(&self) -> Self::Item {
217+
&self.0
218+
}
219+
}
220+
```
221+
198222
### Additional information
199223

200224
For more information, please see [RFC 447].

0 commit comments

Comments
 (0)
Please sign in to comment.