about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-01-17 09:11:20 +0100
committerGitHub <noreply@github.com>2025-01-17 09:11:20 +0100
commit1360e76329a1e3b4590f9f1998779d663e0da003 (patch)
tree63609aaa5bbc9a876366bfc42e077315183d7370
parent82804078d8e5022739d4314a46a67b6023c29e7d (diff)
parent9bdc65866ced6531e988674324ffc72af817c880 (diff)
downloadrust-1360e76329a1e3b4590f9f1998779d663e0da003.tar.gz
rust-1360e76329a1e3b4590f9f1998779d663e0da003.zip
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.
-rw-r--r--compiler/rustc_error_codes/src/error_codes/E0207.md24
1 files changed, 24 insertions, 0 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0207.md b/compiler/rustc_error_codes/src/error_codes/E0207.md
index 95e7c9fc76c..5b35748f472 100644
--- a/compiler/rustc_error_codes/src/error_codes/E0207.md
+++ b/compiler/rustc_error_codes/src/error_codes/E0207.md
@@ -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].