Stream: t-compiler/wg-nll

Topic: Reduced quality of IndexMut error message


Jake Goulding (Mar 12 2019 at 20:54, on Zulip):

Rust 2015

error[E0594]: cannot assign to immutable indexed content
 --> src/main.rs:8:5
  |
8 |     my_map["a"] += 10;
  |     ^^^^^^^^^^^^^^^^^ cannot borrow as mutable
  |
  = help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, i32>`

Rust 2018

error[E0594]: cannot assign to data in a `&` reference
 --> src/main.rs:8:5
  |
8 |     my_map["a"] += 10;
  |     ^^^^^^^^^^^^^^^^^ cannot assign
Jake Goulding (Mar 12 2019 at 20:55, on Zulip):

Is there an issue tracking restoring the earlier note, mentioning IndexMut?

Matthew Jasper (Mar 12 2019 at 20:55, on Zulip):

It's a known issue.

Jake Goulding (Mar 12 2019 at 20:56, on Zulip):

Cool.

Jake Goulding (Mar 12 2019 at 20:56, on Zulip):

Hmm. It was "fixed"? https://github.com/rust-lang/rust/pull/53830

Jake Goulding (Mar 12 2019 at 20:56, on Zulip):

But is not fixed in 1.33

Matthew Jasper (Mar 12 2019 at 20:57, on Zulip):

It looks like it was only fixed for mutable borrows

Jake Goulding (Mar 12 2019 at 20:58, on Zulip):

Ugh. Then it's the other issue

Jake Goulding (Mar 12 2019 at 20:58, on Zulip):
use std::collections::HashMap;

fn main() {
    let mut my_map = HashMap::<&str, i32>::new();
    my_map["a"] += 10;
}
Jake Goulding (Mar 12 2019 at 20:58, on Zulip):

cause it doesn't know to use IndexMut instead of Index

Jake Goulding (Mar 12 2019 at 20:59, on Zulip):

use / try, I guess

Last update: Nov 21 2019 at 13:45UTC