Skip to content

Attempting destructuring of struct reference to mutable variable results in partial moves of fields #102669

Closed as not planned
@cdmistman

Description

@cdmistman

I tried this code:

struct Foo {
    bar: Bar,
}

struct Bar;

fn with_bar(bar: &Bar) {}

fn main() {
    let foo = Foo { bar: Bar };
    let Foo { mut bar } = &foo;
    with_bar(bar);
}

I expected to see this happen: The above should compile with bar being a mutable variable of type &Bar

Instead, this happened: The above fails to compile with bar being a mutable variable of type Bar.

Meta

rustc --version --verbose:

rustc 1.65.0-nightly (015a824f2 2022-08-22)
binary: rustc
commit-hash: 015a824f2dffe32707fceb59c47effaf7b73486c
commit-date: 2022-08-22
host: aarch64-apple-darwin
release: 1.65.0-nightly
LLVM version: 15.0.0
Backtrace

error[[E0308]](https://doc.rust-lang.org/stable/error-index.html#E0308): mismatched types
  --> src/main.rs:12:14
   |
12 |     with_bar(bar);
   |     -------- ^^^
   |     |        |
   |     |        expected `&Bar`, found struct `Bar`
   |     |        help: consider borrowing here: `&bar`
   |     arguments to this function are incorrect
   |
note: function defined here
  --> src/main.rs:7:4
   |
7  | fn with_bar(bar: &Bar) {}
   |    ^^^^^^^^ ---------

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-patternsRelating to patterns and pattern matchingC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions