4 ms·
I agree that whitespace scope delimiting is a bad idea, but I was thinking more about general syntactical clarity. Just because C++ uses every possible typograp
by beerbajay 11y ago
I agree that whitespace scope delimiting is a bad idea, but I was thinking more about general syntactical clarity. Just because C++ uses every possible typographical symbol doesn't mean rust should. Simple example which had me WTFing:
use std::collections::{HashMap, HashSet};
Are all those ::s and {}s adding to clarity?
Why are there no easy to use initializers for maps? It's 2015! I have to do:
let mut map = ::std::collections::HashMap::new();
map.insert("a".to_string(), "b".to_string());
...
or some such nonsense. Oh, right, I have to write a macro and basically learn another language. Also, using the macro system for basic things like println just seems bonkers to me; use macros when you really, really need to do something special, not everywhere.
- dbaupp 11y agoYou personally don't have to write the macro: cargo makes it super easy to use ones others have written, e.g. https://crates.io/crates/maplit/ https://crates.io/crates/maplit/.
- shmerl 11y agoYeah, I also miss initializer lists[1] in Rust. Not sure why they weren't implemented, but it's a very good feature which is pleasant to use in C++11 and higher. It's way neater syntax than writing different macros like for vector and other containers. Is it something that developers just never got to implement, or they didn't want to do it for some reason? [1]: https://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists https://en.wikipedia.org/wiki/C%2B%2B11#Initializer_lists
- steveklabnik 11y agoPrintln is a macro because it needs to be: it does compile-time checking of the format string.
- shmerl 11y agoDo you think it's feasible to implement initializer lists in Rust, or there is some language limitation which makes it hard? I.e. I'd prefer to write: let v: Vec<i32> = {1, 2, 3, 4, 5}; Instead of let v = vec![1, 2, 3, 4, 5];
- steveklabnik 11y agoI'm not sure, other than that because {} evaluates to a value, this already looks very close to valid Rust; I thought it was for a minute, but realized that it's not quite. I'm not sure I personally feel that the first is particularly better than the latter, but it should have to be by a large margin to justify adding new syntax to the language.
- shmerl 11y agoI can explain why I think initializer lists style is better. You can have a standard / uniform syntax for initialization. And it's using already defined syntax for types to actually indicate what you are creating. Compare it to the current one. It's necessary to create a new syntax for each type (that you want to enable for such initialization). I.e. a macro. This macro also is different from already existing syntax which indicates the type (i.e. you have Vec and you have vec!). That's why I think current approach is more cumbersome and initializer lists style is more streamlined and elegant. > it should have to be by a large margin to justify adding new syntax to the language. C++11 put some effort into simplifying some cumbersome syntax constructs. It makes things easier to read and write which pays off.
- dbaupp 11y agoInitialistion 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.