TryBugHunt

cannot move out of borrowed content

cannot move out of borrowed content in Rust means you attempted to transfer ownership of a value that you only have a reference (&T or &mut T) to, which would leave the referenced memory in an uninitialized state.

RustPattern: Scope and closure bugs

How do I fix cannot move out of borrowed content?

To fix cannot move out of borrowed content in Rust, locate the line causing the error and check the values right before it runs. Most occurrences happen because of extracting a string from a borrowed struct field. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

What does cannot move out of borrowed content mean?

Rust strictly tracks ownership. When you borrow a reference to a struct or collection, you do not own the underlying memory. Moving a non-Copy type (such as String or Vec) out of a reference would deallocate or invalidate the original owner's data.

Why does cannot move out of borrowed content happen?

1. Extracting a String from a borrowed struct field

Moving field 'name' out of &User leaves the struct partially moved and broken.

Breaks

fn get_name(user: &User) -> String {
    user.name // cannot move out of borrowed user!
}

Works

fn get_name(user: &User) -> String {
    user.name.clone() // clone to create a new owned instance
}

2. Taking an item out of a vector via indexing

Indexing vec[0] produces a reference &T, not an owned T.

Breaks

let item = vec[0]; // cannot move out of vec[0]!

Works

let item = vec.remove(0); // or vec.pop(), or vec[0].clone()

How to find it in your own code

Use .clone() to produce an owned copy if the type implements Clone. If you own the container, use methods designed for extraction like Option::take(), std::mem::replace(), or Vec::remove(). Implement the Copy trait for lightweight types.

Try it: fix this bug

medium

Match arm unconditionally matches because variable name binds a new pattern instead of comparing

fn match_expected_code(actual: i32, expected: i32) -> bool {
    match actual {
        // 'expected' here binds a new local variable shadowing the parameter!
        expected => true,
        _ => false,
    }
}

fn main() {
    println!("{}", match_expected_code(404, 200));
}
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error or pattern. Find the bug and fix it in your browser — no install, no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.