Why can't I move a captured variable into a closure?

67 Views Asked by At

This snippet:

struct TaskChain
{
    
}

impl TaskChain
{

    pub fn add_task<T, A>(mut self, job: T, arguments: A) -> Self
        where
            T: Fn(usize, A) + 'static,
            A: Clone + 'static,
    {
        let arg_copy = arguments.clone();
        self.run_task(
            move |num| { job(num, arg_copy) }
        );

        self
    }
    
    pub fn run_task<T>(&self, job: T)
    where
            T: Fn(usize) + 'static,
    {
        job(0)
    }
}

Produces this error:

error[E0507]: cannot move out of `arg_copy`, a captured variable in an `Fn` closure
  --> src/main.rs:16:35
   |
14 |         let arg_copy = arguments.clone();
   |             -------- captured outer variable
15 |         self.run_task(
16 |             move |num| { job(num, arg_copy) }
   |             ----------            ^^^^^^^^ move occurs because `arg_copy` has type `A`, which does not implement the `Copy` trait
   |             |
   |             captured by this `Fn` closure

I don;t understand why, I am capturing the variable, so should I not be allowed to pass it further along into the closure?

0

There are 0 best solutions below