Skip to content
Latchkey

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.

cargo
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` trait

Common 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.

Rust
let owned = config.clone();   // needs Config: Clone

Work with the reference, or take ownership upstream

  1. Operate on &Config rather than an owned Config if you only read it.
  2. Use std::mem::take/replace to move a field out while leaving a default behind.
  3. 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 Clone on types you frequently need owned copies of.
  • Use mem::take/mem::replace for owned-by-value moves out of structs.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →