rust ownership ai_generated true

error[E0382]: borrow of moved value: `x`

ID: rust/e0382-use-after-move

Also available as: JSON · Markdown
94%Fix Rate
96%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

Value has been moved to another owner and cannot be used anymore.

generic

Workarounds

  1. 92% success Clone the value before moving if you need to use it again
    let y = x.clone(); move_fn(x); use(y);

    Sources: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html

  2. 95% success Use references instead of moves where possible
    fn process(x: &MyType) instead of fn process(x: MyType)

Dead Ends

Common approaches that don't work:

  1. Using unsafe to read the moved memory 95% fail

    Undefined behavior; memory may have been freed or reused

  2. Making everything Copy 60% fail

    Not all types can be Copy (heap-allocated types like String, Vec)

Error Chain

Preceded by:
Frequently confused with: