Skip to content

Commit 0fde6f6

Browse files
committed
Clarify the error descriptions
1 parent 14aae67 commit 0fde6f6

File tree

1 file changed

+22
-17
lines changed
  • compiler/rustc_error_codes/src/error_codes

1 file changed

+22
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,73 @@
1-
A lifetime of return value does not outlive the function call.
1+
A lifetime of a returned value does not outlive the function call.
22

33
Erroneous code example:
44

55
```compile_fail,E0482
66
fn prefix<'a>(
77
words: impl Iterator<Item = &'a str>
8-
) -> impl Iterator<Item = String> {
8+
) -> impl Iterator<Item = String> { // error!
99
words.map(|v| format!("foo-{}", v))
1010
}
1111
```
1212

13-
To fix this error, make the lifetime of the returned value explicit.
13+
To fix this error, make the lifetime of the returned value explicit:
1414

1515
```
1616
fn prefix<'a>(
1717
words: impl Iterator<Item = &'a str> + 'a
18-
) -> impl Iterator<Item = String> + 'a {
18+
) -> impl Iterator<Item = String> + 'a { // ok!
1919
words.map(|v| format!("foo-{}", v))
2020
}
2121
```
2222

23-
[`impl Trait`] feature in return type have implicit `'static` lifetime
24-
restriction and the type implementing the `Iterator` passed to the function
25-
lives just `'a`, so shorter time.
23+
The [`impl Trait`] feature in this example uses an implicit `'static` lifetime
24+
restriction in the returned type. However the type implementing the `Iterator`
25+
passed to the function lives just as long as `'a`, which is not long enough.
2626

2727
The solution involves adding lifetime bound to both function argument and
2828
the return value to make sure that the values inside the iterator
2929
are not dropped when the function goes out of the scope.
3030

31-
Alternative solution would be to guarantee that the `Item` references
31+
An alternative solution would be to guarantee that the `Item` references
3232
in the iterator are alive for the whole lifetime of the program.
3333

3434
```
3535
fn prefix(
3636
words: impl Iterator<Item = &'static str>
37-
) -> impl Iterator<Item = String> {
37+
) -> impl Iterator<Item = String> { // ok!
3838
words.map(|v| format!("foo-{}", v))
3939
}
4040
```
4141

42-
Similar lifetime problem might arise when returning closures.
43-
44-
Erroneous code example:
42+
A similar lifetime problem might arise when returning closures:
4543

4644
```compile_fail,E0482
47-
fn foo(x: &mut Vec<i32>) -> impl FnMut(&mut Vec<i32>) -> &[i32] {
45+
fn foo(
46+
x: &mut Vec<i32>
47+
) -> impl FnMut(&mut Vec<i32>) -> &[i32] { // error!
4848
|y| {
4949
y.append(x);
5050
y
5151
}
5252
}
5353
```
5454

55-
Analogically, solution here is to use explicit return lifetime
55+
Analogically, a solution here is to use explicit return lifetime
5656
and move the ownership of the variable to the closure.
5757

5858
```
59-
fn foo<'a>(x: &'a mut Vec<i32>) -> impl FnMut(&mut Vec<i32>) -> &[i32] + 'a {
59+
fn foo<'a>(
60+
x: &'a mut Vec<i32>
61+
) -> impl FnMut(&mut Vec<i32>) -> &[i32] + 'a { // ok!
6062
move |y| {
6163
y.append(x);
6264
y
6365
}
6466
}
6567
```
6668

67-
- [`impl Trait`]: https://doc.rust-lang.org/reference/types/impl-trait.html
68-
- [RFC 1951]: https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html
69+
To better understand the lifetime treatment in the [`impl Trait`],
70+
please see the [RFC 1951].
71+
72+
[`impl Trait`]: https://doc.rust-lang.org/reference/types/impl-trait.html
73+
[RFC 1951]: https://rust-lang.github.io/rfcs/1951-expand-impl-trait.html

0 commit comments

Comments
 (0)