Ad Space — Top Banner

E0382

Rust Programming Language

Severity: Minor

What Does This Error Mean?

Rust error E0382 means you tried to use a value after it was already moved somewhere else. In Rust, each value has exactly one owner at a time. When you pass a value to a function or assign it to another variable, ownership moves. After the move, the original variable is no longer valid and cannot be used. Fix it by cloning the value, borrowing it with a reference, or restructuring your code.

Affected Models

  • Rust 1.0 and later
  • All Rust editions (2015, 2018, 2021)
  • cargo build and rustc

Common Causes

  • Passing a value to a function without using a reference, which moves ownership into the function
  • Assigning a value to a new variable, which transfers ownership away from the original
  • Using a value inside a closure that captures it by move
  • Trying to use a value after it was returned from or consumed by a method
  • Moving a value inside a loop and then trying to use it in the next iteration

How to Fix It

  1. Read the error to find which variable was moved and where the move happened. The compiler shows both lines.

    The compiler output labels the move location and the later use. Start by understanding those two lines.

  2. If you need to pass a value to a function but keep using it, pass a reference instead. Change my_function(value) to my_function(&value).

    This is called borrowing. The function gets to read the value without taking ownership.

  3. If the function needs its own copy, clone the value before passing it: my_function(value.clone()).

    Cloning creates a full independent copy. This costs memory and time, so prefer borrowing when possible.

  4. If you are moving a value inside a loop, clone it at the start of each iteration so each pass gets its own copy.

    Example: let item = original.clone(); — do this inside the loop before using item.

  5. If the type implements the Copy trait (like integers and booleans), no move happens at all — the value is automatically copied.

    Only heap-allocated types like String and Vec move. Simple stack types like i32 and bool are always copied.

When to Call a Professional

E0382 is a compile-time error that will not let your program run until resolved. This error is one of Rust's most important safety features — it prevents use-after-free bugs. The compiler message tells you exactly which variable was moved and on which line.

Frequently Asked Questions

Why does Rust have ownership rules at all?

Ownership is how Rust guarantees memory safety without needing a garbage collector. In other languages, two parts of the program can hold references to the same data, leading to hard-to-find bugs. Rust's ownership rules make these bugs impossible at compile time, so your program is safer and faster.

What is the difference between moving and borrowing?

Moving transfers full ownership to the new location. The original variable can no longer be used. Borrowing lets another part of the code use the value temporarily, using a reference (&). The original owner keeps ownership. Borrowing is usually the better choice — it avoids unnecessary copying and keeps your code flexible.

Does cloning always fix E0382?

It fixes the error, but it is not always the right fix. Cloning creates a full deep copy of the data, which uses extra memory and time. Prefer borrowing with references when the function only needs to read the value. Only clone when you genuinely need two independent copies of the data.