|
1 |
| -An underscore `_` character has been used as the identifier for a lifetime, |
2 |
| -or a const generic has been borrowed without an explicit lifetime. |
| 1 | +An underscore `_` character has been used as the identifier for a lifetime. |
3 | 2 |
|
4 |
| -Erroneous example with an underscore: |
| 3 | +Erroneous example: |
5 | 4 | ```compile_fail,E0106,E0637
|
6 |
| -fn foo<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {} |
7 |
| - // ^^ `'_` is a reserved lifetime name |
8 |
| -``` |
9 |
| -Lifetimes are named with `'ident`, where ident is the name of the lifetime or |
10 |
| -loop. The `_` character, which represents the ignore pattern, cannot be used |
11 |
| -as the identifier because it is a reserved lifetime name. To fix |
12 |
| -this, use a lowercase letter, or a series of lowercase letters as the lifetime |
13 |
| -identifier. Often a single lowercase letter, such as `'a`, is sufficient. For |
14 |
| -more information, see [the book][bk-no]. |
15 |
| - |
16 |
| -Corrected underscore example: |
17 |
| -``` |
18 |
| -fn foo<'a>(str1: &'a str, str2: &'a str) -> &'a str {} |
19 |
| -``` |
20 |
| - |
21 |
| -Erroneous example with const generic: |
22 |
| -```compile_fail,E0261,E0637,E0658 |
23 |
| -struct A<const N: &u8>; |
24 |
| -//~^ ERROR `&` without an explicit lifetime name cannot be used here |
25 |
| -trait B {} |
26 |
| -
|
27 |
| -impl<const N: &u8> A<N> { |
28 |
| -//~^ ERROR `&` without an explicit lifetime name cannot be used here |
29 |
| - fn foo<const M: &u8>(&self) {} |
30 |
| - //~^ ERROR `&` without an explicit lifetime name cannot be used here |
| 5 | +fn longest<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str { |
| 6 | + //^^ `'_` is a reserved lifetime name |
| 7 | + if str1.len() > str2.len() { |
| 8 | + str1 |
| 9 | + } else { |
| 10 | + str2 |
| 11 | + } |
31 | 12 | }
|
32 |
| -
|
33 |
| -impl<const N: &u8> B for A<N> {} |
34 |
| -//~^ ERROR `&` without an explicit lifetime name cannot be used here |
35 |
| -
|
36 |
| -fn bar<const N: &u8>() {} |
37 |
| -//~^ ERROR `&` without an explicit lifetime name cannot be used here |
38 | 13 | ```
|
| 14 | +`'_`, cannot be used as a lifetime identifier because it is a reserved for the |
| 15 | +anonymous lifetime. To fix this, use a lowercase letter such as 'a, or a series |
| 16 | +of lowercase letters such as `'foo`. For more information, see [the book][bk-no]. |
| 17 | +For more information on using the anonymous lifetime in rust nightly, see [the |
| 18 | +nightly book][bk-al]. |
39 | 19 |
|
40 |
| -Const generics cannot be borrowed without specifying a lifetime.The |
41 |
| -compiler handles memory allocation of constants differently than that of |
42 |
| -variables and it cannot infer the lifetime of the borrowed constant. |
43 |
| -To fix this, explicitly specify a lifetime for the const generic. |
44 |
| - |
45 |
| -Corrected const generic example: |
| 20 | +Corrected example: |
46 | 21 | ```
|
47 |
| -struct A<const N: &'a u8>; |
48 |
| -
|
49 |
| -trait B {} |
50 |
| -
|
51 |
| -impl<const N: &'a u8> A<N> { |
52 |
| - fn foo<const M: &'a u8>(&self) {} |
| 22 | +fn longest<'a>(str1: &'a str, str2: &'a str) -> &'a str { |
| 23 | + if str1.len() > str2.len() { |
| 24 | + str1 |
| 25 | + } else { |
| 26 | + str2 |
| 27 | + } |
53 | 28 | }
|
54 |
| -
|
55 |
| -impl<const N: &'a u8> B for A<N> {} |
56 |
| -
|
57 |
| -fn bar<const N: &'a u8>() {} |
58 | 29 | ```
|
| 30 | + |
59 | 31 | [bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols
|
| 32 | +[bk-al]: https://doc.rust-lang.org/nightly/edition-guide/rust-2018/ownership-and-lifetimes/the-anonymous-lifetime.html |
0 commit comments