forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
20 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,34 @@ | ||
The underscore `_` character has been used as the identifier for a lifetime. | ||
|
||
An underscore `_` character or a numeric literal `u8`, `i32`, `f64`, etc has | ||
been used as the identifier for a lifetime. | ||
|
||
Erroneous code example: | ||
``` | ||
fn some_function<'_>(x: &'_ str, y: &'_ str) -> &'_ str { | ||
fn some_function<'_>(string1: &'_ str, string2: &'_ str) -> &'_ str { | ||
//Some code | ||
} | ||
``` | ||
or Erroneous code example 2: | ||
``` | ||
fn some_function<'u8>(string1: &'u8 str, string2: &'u8 str) -> &'u8 str { | ||
//Some code | ||
} | ||
``` | ||
|
||
Lifetimes are named with 'ident, where ident is the name of the | ||
lifetime or loop. The `_` character, which represents the ignore pattern, | ||
cannot be used because it is a reserved lifetime name. | ||
To fix this, use a single lowercase letter as the | ||
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). | ||
Lifetimes are named with `'ident`, where ident is the name of the lifetime or | ||
loop. The `_` character, which represents the ignore pattern, cannot be used | ||
as the identifier because it is a reserved lifetime name. Numeric literals are | ||
also invalid lifetime identifiers and will cause this error to be thrown. To fix | ||
this, use a series of lowercase letters as the lifetime identifier. Often a | ||
single lowercase letter, such as `'a`, is sufficient. For more information, see | ||
[the book][bk-no]. | ||
|
||
Corrected code example: | ||
``` | ||
fn some_function<'a>(x: &'a str, y: &'a str) -> &'a str { | ||
fn some_function<'a>(string1: &'a str, string2: &'a str) -> &'a str { | ||
//Some code | ||
} | ||
``` | ||
|
||
[bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols | ||
|