Expand docs for E0207
with additional example
This commit is contained in:
parent
d8a64098c9
commit
9bdc65866c
1 changed files with 24 additions and 0 deletions
|
@ -195,6 +195,30 @@ impl<'a> Contains for Foo {
|
||||||
Please note that unconstrained lifetime parameters are not supported if they are
|
Please note that unconstrained lifetime parameters are not supported if they are
|
||||||
being used by an associated type.
|
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
|
### Additional information
|
||||||
|
|
||||||
For more information, please see [RFC 447].
|
For more information, please see [RFC 447].
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue