Skip to content

self-referencial struct: mutable borrow not dropped? #138496

Open
@axos88

Description

@axos88

I tried this code:

struct Foo<'a> {
  name: Option<&'a str>,
  foo: String
}

impl<'a> Foo<'a> {
  fn op1(&'a mut self) {
    // self.name = Some(&self.foo);
  }

  fn op2(&self) {
  }
}


fn x() {
  let mut foo = Foo { name: None, foo: "foo".to_string() };

  foo.op1();
  foo.op2(); // Cannot borrow as immutable because borrowed as mutable
}

I expected to see this happen: should compile.

Instead, this happened: compiler seems to think foo.op2() hangs onto the mutable borrow in op1

Meta

rustc --version --verbose:

$ rustc --version --verbose
rustc 1.87.0-nightly (cbfdf0b01 2025-03-13)
binary: rustc
commit-hash: cbfdf0b014cb04982a9cbeec1578001001167f6e
commit-date: 2025-03-13
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0


Metadata

Metadata

Assignees

No one assigned

    Labels

    C-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions