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

Diagnose it: linker, target, or memory

Rust build failures in CI that are not compiler errors are usually a missing system linker or library, a target that is not installed, or the compiler being killed for memory.

Terminal
rustup target list --installed
cc --version || echo "no C toolchain: install build-essential"
free -h

# exit 137 during codegen is an out-of-memory kill, not a compile error
cargo build -j 2   # fewer parallel codegen units uses less memory

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.

Frequently asked questions

What causes Rust "error[E0507]: cannot move out of borrowed content" in CI?
There are 2 common causes: dereferencing a reference to take ownership and moving a field out of a borrowed struct. Writing *reference (or destructuring by value) on a non-Copy type behind &/&self tries to move it out, which is not allowed.
How do I fix Rust "error[E0507]: cannot move out of borrowed content" in CI?
There are 2 fixes depending on which cause you have: clone instead of moving and work with the reference, or take ownership upstream. Work through them in order, since the first is the most common.
What does Rust "error[E0507]: cannot move out of borrowed content" in CI actually mean?
cargo build fails with error[E0507]: cannot move out of X which is behind a shared reference (or ...
How do I stop Rust "error[E0507]: cannot move out of borrowed content" in CI happening again?
Borrow (&T) when you only need to read; clone only when ownership is required. The prevention section lists 3 changes that keep it from recurring.

Related guides

References

Not every red build is your code. Latchkey repairs the ones that are not, on the runner. Start free → 30-day trial · No credit card