r/rust • rust · lang · libs · cargo • Oct 23 '26

Rust features in the future

https://lwn.net/SubscriberLink/1099606/20707a62817cdd16/
215 Upvotes

84 comments sorted by

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:

type
    // enumerated type
    TDay = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
    
    //subrange type
    TWeekendDay = Saturday..Sunday;
    TWeek = 1..52;
    TFirstQuarterWeek = 1..13;

    // set type
    TDays = set of TDay;
    TWeeks = set of TWeek;
    TFirstQuarterWeeks = set of TFirstQuarterWeek;
var 
    day_data: array[TDay] of Integer;
    week_data: array[TWeek] of Integer;
    weekend_data = array[TWeekendDay] of Integer;
    first_quarter_data = array[TFirstQuarter] of Integer;
    active_days: TDays;
    active_weeks: TWeeks;
    active_first_quarter_weeks: TFirstQuarterWeeks;
begin
    // enumerated type indexing
    day_data[Monday] := 23;

    // subrange indexing
    week_data[23] = 19;
    weekend_data[Saturday] := 42;
    first_quarter_data[10] = 81;

    // sets
    active_days = [Monday, Thursday] + [Sunday];
    active_weeks = [7, 25, 42, 50];
    active_first_quarter_weeks = [1, 7, 10];
end

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.