Skip to content

Commit 9bdc658

Browse files
committed
Expand docs for E0207 with additional example
1 parent d8a6409 commit 9bdc658

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)