Ad Space — Top Banner

E0505

Rust Programming Language

Severity: Moderate

What Does This Error Mean?

E0505 means you tried to move a value (transfer ownership) while a borrow of it is still active. In Rust, if you have a reference (borrow) to a value, you cannot move that value away — the reference would dangle (point to nothing). Rust prevents this at compile time. The fix is to ensure the borrow ends before moving the value, or to clone the value instead of moving it.

Affected Models

  • Rust 1.0 and later
  • All Rust editions

Common Causes

  • Passing a value to a function (which moves it) while a reference to it is still held
  • Returning a value from a function while a borrow of it is still in scope
  • Moving a field out of a struct while holding a reference to the struct
  • A closure captures a value by move while a reference to the value still exists in the outer scope
  • Calling a method that takes ownership (self) on a value that is currently borrowed

How to Fix It

  1. Ensure the borrow ends before the move. Drop the reference (or let it go out of scope) before moving the value.

    Use a block scope: { let r = &value; use r; } // borrow ends here then_move(value); // move is now safe

  2. Clone the value to create an independent owned copy that can be moved without affecting the borrowed original.

    let owned = value.clone(); // owned is independent then_move(owned); // safe to move — value still borrowed for reading

  3. Restructure the code to finish all borrows before any moves. Batch operations: do all reads first (while borrowed), then release borrows, then do moves.

    Think of it as: finish looking at the data, put it down (release borrows), then hand it off (move).

  4. If you are moving a field out of a struct while the struct is borrowed, consider using Option<T> fields so you can take() the field: let x = struct_instance.field.take();

    Option::take() replaces the value with None and returns the owned value — no move-out-of-struct issue.

  5. If you need to pass both an owned value and a reference to it, pass the owned value first (getting back the reference if needed) or restructure to not need both at once.

    In Rust you fundamentally cannot have an owner and borrower active simultaneously when ownership needs to transfer.

When to Call a Professional

E0505 is Rust's ownership system preventing a use-after-free bug. The fix is to ensure the borrow ends before the move, or to use .clone() to create a new owned copy. Rust's compiler shows exactly where the borrow starts and where the move conflicts with it.

Frequently Asked Questions

What does 'move' mean in Rust?

When you assign a value, pass it to a function, or return it, ownership transfers — this is called a move. After a move, the original variable is invalid and cannot be used. Moving is how Rust tracks who is responsible for cleaning up a value (dropping it from memory). This is different from languages like C# or Java where assignment copies a reference — in Rust it transfers ownership.

Why can I not move a value while it is borrowed?

Because the borrow is a reference — a pointer to the value's memory location. If the value moves, it might end up in different memory, making the reference point to nowhere (a dangling pointer). This is exactly the kind of memory safety bug that Rust prevents. By disallowing the move while the borrow is active, Rust guarantees the reference is always valid.

When should I use clone() vs restructuring borrows?

Use clone() when the clone cost is acceptable and the fix is simple. Use restructuring when the clone would be expensive (large data structures) or when the logic can naturally be reorganized to avoid the conflict. Rust's ownership model often encourages a better design — if you frequently need to clone, it might be worth redesigning ownership. For prototyping and small data, clone() is perfectly fine.