Inspired by a common stumbling block with Iterator::flat_map.
This code does not compile:
(0..10).flat_map(|i| (0..i).map(|j| i + j))
error: `i` does not live long enough
(playground code)
The problem: i is a local variable and captured by reference in a closure we're trying to return. It's an integer, its data size is equal or smaller as a value instead of a reference.
Can rustc infer that the closure should capture i by "move" here (Copy)?
Inspired by a common stumbling block with
Iterator::flat_map.This code does not compile:
(playground code)
The problem:
iis a local variable and captured by reference in a closure we're trying to return. It's an integer, its data size is equal or smaller as a value instead of a reference.Can rustc infer that the closure should capture
iby "move" here (Copy)?