3 ms·
Initialistion from initializer lists in C++ basically rely on the = operator being able to do arbitrary work, e.g. allocations and generally manipulating the he
by dbaupp 11y ago
Initialistion from initializer lists in C++ basically rely on the = operator being able to do arbitrary work, e.g. allocations and generally manipulating the heap in arbitrary ways. Rust tries to avoid implicit performance sinks like that, and so doesn't have constructors in the same manner as C++.
That said, in future, it is likely to be possible for things like `Vec::from_array([1, 2, 3, 4])` and `HashMap::from_array([[1, 2], [3, 4]])` to work.
- shmerl 11y agoRust doesn't need to take the C++ approach to implement it, but it can still use the same syntax which is simply much neater. HashMap::from_array([[1, 2], [3, 4]]) looks horrible in comparison. Exact implementation is another question. That's why asked, whether it's feasible or it's something hard to fit into the language.
- dbaupp 11y agoI don't see how Rust can get the same syntax as C++ without having the implicit performance sink I mentioned: creating vectors and hashmaps needs to allocate, and so letting this happen implicitly is not great. In fact, the syntax was pretty much the whole meat of my comment.
- shmerl 11y agoWhy can't Rust do what it does already with macros, except using the simpler syntax described above? I.e. that syntax can mean explicit allocation (it's just a short form for it).