blob: c5f7cf2e337ff3e003622e1d9e00499016ee75bb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#### Note: this error code is no longer emitted by the compiler.
Reference's lifetime of borrowed content doesn't match the expected lifetime.
Erroneous code example:
```compile_fail
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'static str {
if maybestr.is_none() {
"(none)"
} else {
let s: &'a str = maybestr.as_ref().unwrap();
s // Invalid lifetime!
}
}
```
To fix this error, either lessen the expected lifetime or find a way to not have
to use this reference outside of its current scope (by running the code directly
in the same block for example?):
```
// In this case, we can fix the issue by switching from "static" lifetime to 'a
pub fn opt_str<'a>(maybestr: &'a Option<String>) -> &'a str {
if maybestr.is_none() {
"(none)"
} else {
let s: &'a str = maybestr.as_ref().unwrap();
s // Ok!
}
}
```
|