|
1 |
| -An underscore `_` character has been used as the identifier for a lifetime. |
| 1 | +`'_` lifetime name or `&T` without an explicit lifetime name has been used |
| 2 | +on illegal place. |
2 | 3 |
|
3 | 4 | Erroneous code example:
|
4 | 5 |
|
5 | 6 | ```compile_fail,E0106,E0637
|
6 |
| -fn longest<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str { |
7 |
| - //^^ `'_` is a reserved lifetime name |
| 7 | +fn underscore_lifetime<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str { |
| 8 | + //^^ `'_` is a reserved lifetime name |
8 | 9 | if str1.len() > str2.len() {
|
9 | 10 | str1
|
10 | 11 | } else {
|
11 | 12 | str2
|
12 | 13 | }
|
13 | 14 | }
|
| 15 | +
|
| 16 | +fn and_without_explicit_lifetime<T>() |
| 17 | +where |
| 18 | + T: Into<&u32>, |
| 19 | + //^ `&` without an explicit lifetime name |
| 20 | +{ |
| 21 | +} |
14 | 22 | ```
|
15 | 23 |
|
16 |
| -`'_`, cannot be used as a lifetime identifier because it is a reserved for the |
17 |
| -anonymous lifetime. To fix this, use a lowercase letter such as 'a, or a series |
18 |
| -of lowercase letters such as `'foo`. For more information, see [the |
19 |
| -book][bk-no]. For more information on using the anonymous lifetime in rust |
20 |
| -nightly, see [the nightly book][bk-al]. |
| 24 | +First, `'_` cannot be used as a lifetime identifier in some places |
| 25 | +because it is a reserved for the anonymous lifetime. Second, `&T` |
| 26 | +without an explicit lifetime name cannot also be used in some places. |
| 27 | +To fix them, use a lowercase letter such as `'a`, or a series |
| 28 | +of lowercase letters such as `'foo`. For more information about lifetime |
| 29 | +identifier, see [the book][bk-no]. For more information on using |
| 30 | +the anonymous lifetime in Rust 2018, see [the Rust 2018 blog post][blog-al]. |
21 | 31 |
|
22 | 32 | Corrected example:
|
23 | 33 |
|
24 | 34 | ```
|
25 |
| -fn longest<'a>(str1: &'a str, str2: &'a str) -> &'a str { |
| 35 | +fn underscore_lifetime<'a>(str1: &'a str, str2: &'a str) -> &'a str { |
26 | 36 | if str1.len() > str2.len() {
|
27 | 37 | str1
|
28 | 38 | } else {
|
29 | 39 | str2
|
30 | 40 | }
|
31 | 41 | }
|
| 42 | +
|
| 43 | +fn and_without_explicit_lifetime<'foo, T>() |
| 44 | +where |
| 45 | + T: Into<&'foo u32>, |
| 46 | +{ |
| 47 | +} |
32 | 48 | ```
|
33 | 49 |
|
34 | 50 | [bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols
|
35 |
| -[bk-al]: https://doc.rust-lang.org/nightly/edition-guide/rust-2018/ownership-and-lifetimes/the-anonymous-lifetime.html |
| 51 | +[blog-al]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html#more-lifetime-elision-rules |
0 commit comments