Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primitive variable does not live long enough

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?

like image 495
Igor Chubin Avatar asked Mar 25 '26 08:03

Igor Chubin


1 Answers

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 move keyword: take the ownership.
like image 56
Boiethios Avatar answered Mar 28 '26 03:03

Boiethios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!