问题
There is an error in this piece of code:
let a: Vec<_> = (1..10).flat_map(|x| (1..x).map(|_| x)).collect();
The error message:
error[E0597]: `x` does not live long enough
--> src/main.rs:2:57
|
2 | let a: Vec<_> = (1..10).flat_map(|x| (1..x).map(|_| x)).collect();
| --- ^- - borrowed value needs to live until here
| | ||
| | |borrowed value only lives until here
| | borrowed value does not live long enough
| capture occurs here
But why?
Is is a primitive type, i.e. it should be cloned anyway.
What do I understand wrong?
回答1:
This does not work because you capture x by reference when you do map(|_| x). x is not a variable local to the closure, so it is borrowed. To not borrow x, you must use the move keyword:
let a: Vec<_> = (1..10).flat_map(|x| (1..x).map(move |_| x)).collect();
But this is more idiomatic to write (for the same output):
use std::iter::repeat;
let b: Vec<_> = (2..10).flat_map(|x| repeat(x).take(x - 1)).collect();
Concerning the "why" question: some people could want to borrow a copyable data, so the capturing rules are the same:
- Default: by reference,
- With the
movekeyword: take the ownership.
来源:https://stackoverflow.com/questions/53064925/primitive-variable-does-not-live-long-enough