Include atoms, or symbolic constants in Rust

I programming mostly with Rust, elixir and Clojure, and one of the things I miss the most in Rust is the use o symbolic constants like :key, :my_key, :a_value. Does anyone else miss this? Specially for pattern matching and hashmaps keys. I think that also in Rust it would be pretty interesting to have the ability to create an inferred hashmap from the syntax {:key1 "value", : key2 "value2"}. Using macros to do this is not very fun.

Symbolic constants make more sense in languages without strict type checking. We have enums that you can use for this purpose.


I never understood all the buzz about symbols/atoms. They really don't provide much type safety. Since they can be created ad-hoc just like string literals can be, the only real advantage that I can see is that uniquing could be guaranteed for atoms. Which is nice because it can be used for O(1) comparisons; however, there needs to be a global (and therefore probably locked) hash table (or whatever) storing them, which might make their creation more expensive.

But in a dynamically-typed language, runtimes can already be written with string interning in mind, so that's not really a distinguishing feature. And in a statically-typed language, creating hash maps from symbols to values is inferior to creating a struct with named fields, which is checked at compile time. (And, as alice mentioned, for generating sets there are enums, also checked at compile time and being comparable in O(1) in the simple case when they do not contain associated data.)

1 Like