How to Derive Clone and Copy on Struct with Vector of integers?

Viewed 447

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.

1 Answers

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.

Related