You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#12825 is preventing implementing IndexMut for Vec, HashMap, &c., but once that’s fixed, simply doing vec[index] = value; will work. The reason slicing uses an explicit mut but not indexing is because slicing directly returns a reference/slice (e.g., vec[a..b] returns &[T], vec[mut a..b] returns &mut [T]), while indexing just returns a value (e.g., vec[a] returns T). However, it doesn’t move the value out of the collection, it just dereferences a reference to it (i.e., vec[a] = *vec.index(&a) or *vec.index_mut(&a)). The problem is figuring out which of index and index_mut to use, which is represented by the issue #12825.
Mutably indexing a
Vec
(and probably lots of other collections) is weird.On one hand, you can dereference a mutable pointer:
Which is ugly and inconsistent with immutable indexing (
vec[index]
)With the new slicing syntax you can do this:
Which is arguably nicer. But wouldn't something like:
Be even better and more consistent?
The text was updated successfully, but these errors were encountered: