]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0637.md
Rollup merge of #89876 - AlexApps99:const_ops, r=oli-obk
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0637.md
1 `'_` lifetime name or `&T` without an explicit lifetime name has been used
2 on illegal place.
3
4 Erroneous code example:
5
6 ```compile_fail,E0106,E0637
7 fn underscore_lifetime<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {
8                      //^^ `'_` is a reserved lifetime name
9     if str1.len() > str2.len() {
10         str1
11     } else {
12         str2
13     }
14 }
15
16 fn and_without_explicit_lifetime<T>()
17 where
18     T: Into<&u32>,
19           //^ `&` without an explicit lifetime name
20 {
21 }
22 ```
23
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].
31
32 Corrected example:
33
34 ```
35 fn underscore_lifetime<'a>(str1: &'a str, str2: &'a str) -> &'a str {
36     if str1.len() > str2.len() {
37         str1
38     } else {
39         str2
40     }
41 }
42
43 fn and_without_explicit_lifetime<'foo, T>()
44 where
45     T: Into<&'foo u32>,
46 {
47 }
48 ```
49
50 [bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols
51 [blog-al]: https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html#more-lifetime-elision-rules