Hey guys, I have this situation where I have optional structs that include other optional structs. And I am trying to move and Option to a new place, but accidentally, I introduce sharing of a reference and the compiler complains:
line: some_bar.string = Some(*string);
cannot move out of `*string` which is behind a shared reference
move occurs because `*string` has type `String`, which does not implement the `Copy` trait
consider cloning the value if the performance cost is acceptable: `.clone()`
But - on the next line I do a drop of the value that the reference string points to:
Can you please explain to me, why am I wrong? I hoped that the compiler would see that I am dropping the container which contains the value referenced in string and thus would allow me to do the move...
On line some_bar.result_strings = some_baz.temporary_string_storage; it says:
cannot move out of `some_baz.temporary_string_storage` which is behind a mutable reference
move occurs because `some_baz.temporary_string_storage` has type `Vec<String>`, which does not implement the `Copy` trait
consider cloning the value if the performance cost is acceptable: `.clone()`
Perhaps these examples are too contrived to reflect your actual situation. In which case, you could check out the replace_with crate. It allows you to temporary move out of a place, so long as you provide some way to reinitialize that place later (or abort to avoid UB).