]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0261.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0261.md
1 An undeclared lifetime was used.
2
3 Erroneous code example:
4
5 ```compile_fail,E0261
6 // error, use of undeclared lifetime name `'a`
7 fn foo(x: &'a str) { }
8
9 struct Foo {
10     // error, use of undeclared lifetime name `'a`
11     x: &'a str,
12 }
13 ```
14
15 These can be fixed by declaring lifetime parameters:
16
17 ```
18 struct Foo<'a> {
19     x: &'a str,
20 }
21
22 fn foo<'a>(x: &'a str) {}
23 ```
24
25 Impl blocks declare lifetime parameters separately. You need to add lifetime
26 parameters to an impl block if you're implementing a type that has a lifetime
27 parameter of its own.
28 For example:
29
30 ```compile_fail,E0261
31 struct Foo<'a> {
32     x: &'a str,
33 }
34
35 // error,  use of undeclared lifetime name `'a`
36 impl Foo<'a> {
37     fn foo<'a>(x: &'a str) {}
38 }
39 ```
40
41 This is fixed by declaring the impl block like this:
42
43 ```
44 struct Foo<'a> {
45     x: &'a str,
46 }
47
48 // correct
49 impl<'a> Foo<'a> {
50     fn foo(x: &'a str) {}
51 }
52 ```