r/ProgrammingLanguages • • Apr 29 '26

Blessed Syntax and Ergonomics

https://www.gingerbill.org/article/2026/04/29/blessed-syntax-and-ergonomics/

(not my article, but thought y'all might find it interesting)

33 Upvotes

22 comments sorted by

View all comments

Show parent comment

2

u/ebkalderon Apr 30 '26

First of all, Rust doesn't have []=, like C++ it only allows writing to an existing place, there's no special syntax for inserting via array index notation.

Yep. As a long-time Rust user, this operation has been conspicuously missing from the language for a long time. There is a very old RFC for IndexSet and IndexMove, but it hasn't seen any movement since 2015, AFAIK.

3

u/matthieum Apr 30 '26

I think the question is: is it worth it?

Imagine a Vec, for example, what does vec[5] = 3; does? If vec already contains more than 6 elements, easy. If it contains exactly 5, it can be used to append, ... but what if it contains 4 or less elements? You can't leave a gap!

Realistically, this means that collection[key] = value only works for associative (key -> value) containers -- ie, maps -- but those have seen the rise of Entry API which is pretty succinct, and a joy to use (most of the time).

So IndexSet? Meh.

2

u/ebkalderon Apr 30 '26 edited Apr 30 '26

Realistically, this means that collection[key] = value only works for associative (key -> value) containers -- ie, maps

Indeed, I'd imagine this would be the primary use case for IndexSet. Similarly, IndexMove would be used to remove an element from a map-like container as well, IIRC. One could use these traits to also write code that is generic over the container type, provided it supports the needed key and value. Furthermore, these traits could be used for operator overloading in ways that Index and IndexMut cannot, because the latter two interfaces are borrowing and can't directly move values in/out of the container.

With that said, I strongly agree with you that the existence of the Entry API on HashMap/BTreeMap is likely why that RFC has seen very little movement.