Skip to content

Odd loaning errors when similar code works #4666

Closed
@alexcrichton

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.

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