error use-after-move
Значение использовано после перемещения.
What it means#
Values that are not Copy are moved on assignment, argument passing and return, and the source can no longer be used (§3.9, §7.1). Using a moved value is a compile-time error.
Example#
fn main() {
let xs = [1, 2, 3]
let ys = xs
print(xs.len()) // <- use-after-move
}use-after-move
A closure body can run any number of times, so it may not move a variable it captured from outside (that would free the value again on the next call):
fn eat(xs: List<i32>) -> usize => xs.len()
fn main() {
let a = [1, 2, 3]
let f = () => eat(a) // <- use-after-move: moves the captured `a` on every call
print(f() + f())
}Ownership-taking calls such as Rc.new(x), freeze(x) and List.push(x) move their argument as well. A spawn closure runs exactly once and may consume its captures.
How to fix#
fn main() {
let xs = [1, 2, 3]
let ys = xs.clone()
print(xs.len())
}Copy explicitly with .clone(), borrow with &xs instead of moving, or stop using the old name. For a closure, pass a.clone() / take the list by & parameter, or create the value inside the closure. Types with @derive(Copy) and primitives are copied automatically.