Rust trying to move a struct with Copy


#1

I tried to come up with a minimal example on rust playground here: https://gist.github.com/rust-play/c46c6773968540c25d66d57ff1420b28

I’m getting the error “cannot move out of borrowed context” in reference to the line

let _e: Edit<'a, T> = self.edit;

but Edit has #[derive(Copy, Clone)] so shouldn’t it be trying to copy instead of move?


#2

What does Edits definition look like?


#3

I’ll just copy the code:

#[derive(Copy, Clone)]
pub enum Edit<'a, T: 'a + Eq> {
    Foo(&'a T),
}

struct GridEntry<'a, T: 'a + Eq> {
    edit: Edit<'a, T>
}

impl<'a, T: 'a + Eq> GridEntry<'a, T> {
    fn f(&self) {
        let _e: Edit<'a, T> = self.edit;
    }
}

fn main() {
}

#4

The derived implementation looks like

impl<'a, T: 'a + Eq + Clone> Clone for Edit<'a, T> { ... }

The deriving machinery doesn’t know that the T: Clone bound isn’t actually necessary. You’ll probably want to manually implement Copy and Clone to avoid the extra bound.


#5

Ah thanks! I actually don’t even want the thing pointed to the reference to be cloned so it looks like I would have had to make my own Clone implementation anyway.


#6

There are several github issues around this. One of them is: