#is there a difference between (1..6) and (1..=5)?

8 messages · Page 1 of 1 (latest)

faint abyss
#

They're both iterator producing 1 2 3 4 5 but their type is different, the first one is a Range, the other one is a RangeInclusive

lyric solar
#

They do the same thing, but the 1..6 takes up a bit less memory and the 1..=5 style can handle going all the way to the last value in the type (1..=u8::MAX goes all the way up to 255, while there is no a..b that goes up that high).

late drift
#

as well as this, sometimes it just

#

readability wise

#

makes more sense to use one over the other

#

for example, for a dice roll, rand::gen_range(1..7) just makes your brain pause

#

to read it

#

rand::gen_range(1..=6) makes more sense when reading the code