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