Skip to content

Borrow checker: borrows in else branch of if let #38159

Closed
@theduke

Description

@theduke

If there already is an issue for this, I apologize. I couldn't find one, but searching for this is a bit hard.

Right now, the borrow checker does not allow a borrow of a variable in the else branch if the let binding borrows the variable.

Example:

  let mut m = HashMap::<i64, bool>::new();
  if let Some(x) = m.get(&10) {
    // do something...
  }
  else {
    m.insert(10, true);
  }

The m.insert is invalid, because the borrow checker assumes an active borrow for the hashmap.

The Some(x) binding is not in scope anymore, though, and there's no way to access x, so this should be allowed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions