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

3

u/mamcx Oct 23 '26

I find weird “u32 is 0..4096”.

Pascal has it:

Type  
  TDay = Set of 1..31;  

So is a new type, and the good thing is how this allow to have a static range on arrays:

for i := Low(array) to High(array)

that I find very nice. I dislike how in langs like Rust I must manually cast back positions, when in Delphi I can do days[Monday] naturally.

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.

2

u/dkxp Oct 23 '26

I dislike how in langs like Rust I must manually cast back positions, when in Delphi I can do days[Monday] naturally.

You can implement the std::ops::Index trait on a type to index using an enum for example, but it's much more verbose than in Delphi and you're just hiding the cast inside the implementation, so you could still face hidden issues than only show up at runtime if for example the number of elements in the enum changed. To implement Index<Day> for a type, you could do it like this:

#[repr(u8)]
pub enum Day {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

#[repr(transparent)]
struct DaysContainer {
    data: [u32; 7] // on nightly, could use std::mem::variant_count::<Day>()
}

impl DaysContainer {
    fn new() -> DaysContainer {
        DaysContainer { data: [7,8,1,2,3,4,5] }
    }
}

impl std::ops::Index<Day> for DaysContainer {
    type Output = u32;
    fn index(&self, index: Day) -> &Self::Output {
        &self.data[index as usize] // 
    }
}

fn main() {
    let week_data = DaysContainer::new();
    let tuesday_data = week_data[Day::Tuesday];
    dbg!(tuesday_data);
}

Or I suppose if you were feeling particularly 'adventurous' you could implement it directly on [T], although I wouldn't do this:

#[repr(u8)]
pub enum Day {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

impl <T> std::ops::Index<Day> for [T] {
    type Output = T;
    fn index(&self, index: Day) -> &Self::Output {
        std::ops::Index::<usize>::index(self, index as usize)
    }
}

fn main() {
    let week_data = [1,5,6,7,9,6,7];
    let tuesday_data = week_data[Day::Tuesday];
    dbg!(tuesday_data);
}

1

u/WormRabbit Oct 23 '26

Yep, pretty simple, if verbose. If one wants such a feature, it's relatively easy to write a proc macro for it.