I have struct with a child struct that has a vector in it: game.requirements.peripherals: Vec<Peripheral>
That game
object is meant to be attached to a Bridge
struct. All that is pretty strightforward.
The problem is that, in the Bridge::new()
method, I construct the game object, and then need to loop through its game.requirements.peripherals
, and finally attach the game
object to the Bridge
and return it.
When I try, I get a
error[E0382]: use of moved value: `game`
--> src/bridge.rs:62:13
|
40 | for required_peripheral in game.requirements.peripherals {
| ----------------------------- value moved here
...
62 | game,
| ^^^^ value used here after partial move
|
= note: move occurs because `game.requirements.peripherals` has type `std::vec::Vec<peripherals::required::RequiredPeripheral>`, which does not implement the `Copy` trait
I think I understand what's happening here. The peripheral is being moved out of game.requirements
for the for
loop. I would have expected ownership to be returned at the end of the loop.
Here is a more full code sample.
let mut network = Network::new();
let mut atlas = Atlas::new();
for required_peripheral in game.requirements.peripherals {
// real_peripheral is *not* attached to either atlas or network.
// Neither is required_peripheral
// discover() returns a dto, and boot_peripheral() and register() both use that information to create their own representations on the network and atlas objects
let real_peripheral = network.discover(required_peripheral);
network.boot_peripheral(real_peripheral);
atlas.register(real_peripheral);
}
// Return the bridge
RfBridge {
game,
atlas,
network
}
I'd really appreciate the help. I am trying to workup a proof of concept that I can hopefully use to convince my workplace to adopt Rust for a few of our projects.