Rust "error[E0507]: cannot move out of borrowed content" in CI
You tried to take ownership of a value you only have a reference to. A move out of borrowed content would leave the borrow pointing at nothing, so the borrow checker rejects it.
What this error means
cargo build fails with error[E0507]: cannot move out of X which is behind a shared reference (or ... behind a & reference), pointing at the move. The build does not complete.
error[E0507]: cannot move out of `*config` which is behind a shared reference
--> src/main.rs:6:18
|
6 | let owned = *config;
| ^^^^^^^ move occurs because `*config` has type `Config`,
| which does not implement the `Copy` traitCommon causes
Dereferencing a reference to take ownership
Writing *reference (or destructuring by value) on a non-Copy type behind &/&self tries to move it out, which is not allowed.
Moving a field out of a borrowed struct
Pattern-matching or accessing a non-Copy field by value through a shared reference attempts a partial move out of borrowed content.
How to fix it
Clone instead of moving
When you only have a borrow but need an owned value, clone it.
let owned = config.clone(); // needs Config: CloneWork with the reference, or take ownership upstream
- Operate on
&Configrather than an ownedConfigif you only read it. - Use
std::mem::take/replaceto move a field out while leaving a default behind. - Change the function to take the value by value if the caller can give ownership.
How to prevent it
- Borrow (
&T) when you only need to read; clone only when ownership is required. - Derive
Cloneon types you frequently need owned copies of. - Use
mem::take/mem::replacefor owned-by-value moves out of structs.