error[E0596]: cannot borrow data in an index of `std::vec::Vec<&mut i32>` as mutable
--> src/lib.rs:4:24
|
4 | let _i: &mut i32 = a[0];
| ^^^^ cannot borrow as mutable
|
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::vec::Vec<&mut i32>`
It's clearly not true that IndexMut is not implemented for Vec, as evidenced by the first implementation.
This looks like a bug to me, but I couldn't find a corresponding issue on GitHub. Am I missing anything here?
The explicit trait call version only works because the explicit type annotation for _i triggers an implicit reborrow. For what it's worth, I suspect that the logic determining whether to choose Index or IndexMut does not take into account that the implicit reborrow will happen, so it comes to the conclusion that this assignment is a move and wrongly selects Index. This suspicion is purely founded on gut feeling, and not on any knowledge about what the compiler is actually doing.
The &mut on the right-hand side forces the compiler to select IndexMut, since it's clear that a mutable place expression is required, so this is not surprising. There are many ways to make this work, but I feel the way currently rejected by the compiler should work as well, and is the most natural approach.