|
1 |
| -The underscore `_` character has been used as the identifier for a lifetime. |
| 1 | + |
| 2 | +An underscore `_` character or a numeric literal `u8`, `i32`, `f64`, etc has |
| 3 | +been used as the identifier for a lifetime. |
2 | 4 |
|
3 | 5 | Erroneous code example:
|
4 | 6 | ```
|
5 |
| -fn some_function<'_>(x: &'_ str, y: &'_ str) -> &'_ str { |
| 7 | +fn some_function<'_>(string1: &'_ str, string2: &'_ str) -> &'_ str { |
| 8 | + //Some code |
| 9 | +} |
| 10 | +``` |
| 11 | +or Erroneous code example 2: |
| 12 | +``` |
| 13 | +fn some_function<'u8>(string1: &'u8 str, string2: &'u8 str) -> &'u8 str { |
6 | 14 | //Some code
|
7 | 15 | }
|
8 | 16 | ```
|
9 | 17 |
|
10 |
| -Lifetimes are named with 'ident, where ident is the name of the |
11 |
| -lifetime or loop. The `_` character, which represents the ignore pattern, |
12 |
| -cannot be used because it is a reserved lifetime name. |
13 |
| -To fix this, use a single lowercase letter as the |
14 |
| -lifetime identifier, such as `'a`. For more information, see [The Rust Book](https://doc.rust-lang.org/stable/book/appendix-02-operators.html#non-operator-symbols). |
| 18 | +Lifetimes are named with `'ident`, where ident is the name of the lifetime or |
| 19 | +loop. The `_` character, which represents the ignore pattern, cannot be used |
| 20 | +as the identifier because it is a reserved lifetime name. Numeric literals are |
| 21 | +also invalid lifetime identifiers and will cause this error to be thrown. To fix |
| 22 | +this, use a series of lowercase letters as the lifetime identifier. Often a |
| 23 | +single lowercase letter, such as `'a`, is sufficient. For more information, see |
| 24 | +[the book][bk-no]. |
15 | 25 |
|
16 | 26 | Corrected code example:
|
17 | 27 | ```
|
18 |
| -fn some_function<'a>(x: &'a str, y: &'a str) -> &'a str { |
| 28 | +fn some_function<'a>(string1: &'a str, string2: &'a str) -> &'a str { |
19 | 29 | //Some code
|
20 | 30 | }
|
21 | 31 | ```
|
22 | 32 |
|
| 33 | +[bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols |
| 34 | + |
0 commit comments