rust-lang / rust-lang/rust

Accessing `Copy` field via `&mut` in `move` closure moves the reference instead of copying the field

Open
#163,034 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-borrow-checker A-closures C-bug needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code:

struct Foo(u8);

fn test(foo: &mut Foo) {
    move || {
        foo.0;
    };

    *foo = Foo(42);
}

I expected to see this happen: It compiles
Instead, this happened:

error[E0382]: use of moved value: `foo`
 --> src/lib.rs:8:5
  |
3 | fn test(foo: &mut Foo) {
  |         --- move occurs because `foo` has type `&mut Foo`, which does not implement the `Copy` trait
4 |     move || {
  |     ------- value moved into closure here
5 |         foo.0;
  |         ----- variable moved due to use in closure
...
8 |     *foo = Foo(42);
  |     ^^^^^^^^^^^^^^ value used here after move
Meta

rustc --version:

1.100.0-nightly (2026-09-18 420ed2a0c3d7225b1744)

@rustbot label A-borrow-checker A-closures

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the minimal Rust reproduction in the issue and confirm the E0382 error. Trace how the borrow checker handles the move closure and the foo.0 field access; done means the example compiles while preserving the later *foo assignment behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.