4 ms·
What do you mean by "it doesn't?" For example say I construct a range [INT_MIN, INT_MAX] which should have a length of 2^32. Am I prohibited from constructing t
by ridiculous_fish 7y ago
What do you mean by "it doesn't?" For example say I construct a range [INT_MIN, INT_MAX] which should have a length of 2^32. Am I prohibited from constructing this range?
- comex 7y agoI misinterpreted your comment; I thought you meant ranges that were so large they couldn't fit in any integer type (presumably of floats). But actually, it's weirder than I thought. RangeInclusive doesn't have an inherent len() method, but one is provided by the ExactSizeIterator trait, which is implemented only when the underlying type is i8, u8, i16, or u16. len() always returns usize, regardless of the original type; thus I believe the reason for that particular list of types is that usize is only guaranteed to be at least 16 bits. But I'm not sure why it's not also implemented for RangeInclusive<usize> and RangeInclusive<isize>...
- ridiculous_fish 7y agoNice, thanks for the complete answer. It's a highly principled (and perhaps frustrating) design, in keeping with Rust's ethos. By way of comparison, Swift overflows and returns a length of 0. Oh well.
- eridius 7y agoRangeInclusive is inclusive. This means `0..=0` has a length of 1. Therefore `0..=usize::MAX` has a length of usize::MAX + 1.
- bbatha 7y agoYou’d use the plain `Range` type for that. https://doc.rust-lang.org/std/ops/struct.Range.html https://doc.rust-lang.org/std/ops/struct.Range.html