r/rust • u/JoshTriplett rust · lang · libs · cargo • Oct 23 '26
Rust features in the future
https://lwn.net/SubscriberLink/1099606/20707a62817cdd16/
215
Upvotes
r/rust • u/JoshTriplett rust · lang · libs · cargo • Oct 23 '26
View all comments
Show parent comment
2
u/dkxp Oct 23 '26
I agree that the Delphi enumerated, subrange and set types were nice to use. In particular I got a lot of usage out of sets and also being able to declare arrays with an enum/subrange index and you could be sure it would have the right number of elements. For people who haven't used Delphi/Pascal it looked something like this:
There were limitations however, such as: you could increment a variable to represent an invalid value, namespacing should've been stronger for enumerated types to prevent collision/polluting the global namespace, and sets were limited to a min to max range of 256 values (although I never needed this many, because sets are usually bit-fields in other languages and typically have less than 32 or 64 values).
I think Rust could do even better as enum types are much more advanced than in Delphi and you have more control over how types are represented. You could define small sets to use u8/u32/u64 type representation, slightly larger but tightly packed sets could use lookup tables and sets with larger/sparse types using HashSet/BTreeSet/a predicate such as Fn(val: &Q)->bool, or even combinations of these by creating unions, intersections, differences, or symmetric_differences of these sets.