Search code examples
rustvectorhashmapclone

How to derive Clone and Copy on struct with a vector?


I feel this problem requires maybe a simple trait annotation, but I am stuck. I want to create clones of these structs during the other part of the program.

use std::collections::HashMap;

#[derive(Debug, Copy, Clone)]
struct NodeMap {
    map: HashMap<usize, Node>,
}

#[derive(Debug, Copy, Clone)]
struct Node {
    destinations: Vec<usize>,
    visits_left: usize,
}

Everything should be clonable. But it says that Vec<usize> nor HashMap<usize, Node> do not implement Copy. I must be missing something because it seems like this shouldn't be an issue.


Solution

  • HashMap and Vec do not implement Copy - they can't be cloned via a bitwise copy.

    That means neither can your structs.

    You can derive Clone without Copy. Just do that.