Skip to content

Odd loaning errors when similar code works #4666

Description

@alexcrichton

For this code

use core::hashmap::linear::LinearMap;

fn fun1() {
  let mut map = LinearMap::new();
  map.insert(1, 2);
  // ok
  let size;
  size = *map.get(&1);
  map.insert(2, size);
}

fn fun2() {
  let mut map = LinearMap::new();
  map.insert(1, 2);
  let size = *map.get(&1); // note: prior loan of immutable granted here
  map.insert(2, size); // error: loan of mutable local variable as mutable conflicts with prior loan
}

fn fun3() {
  let mut map = LinearMap::new();
  map.insert(1, 2);
  // more bad, same as fun2
  map.insert(2, *map.get(&1));
}

fn main() {}

fun1 passes through the compiler, but both fun2 and fun3 generate an error (listed in fun2) and I'm not sure why fun2 is invalid while fun1 is valid.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regions

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions