3 ms·
Range implements the ExactSizedIterator trait, and this is why you can call `(0..5).len()`. When you call len() the ExactSizeIterator just calls size_hint [1].
by ratww 6y ago
Range implements the ExactSizedIterator trait, and this is why you can call `(0..5).len()`. When you call len() the ExactSizeIterator just calls size_hint [1].
The size_hint is implemented here [2]. This is where the "start < end" check happens.
[1] https://doc.rust-lang.org/src/core/iter/traits/exact_size.rs.html#71-134 https://doc.rust-lang.org/src/core/iter/traits/exact_size.rs...
[2] https://doc.rust-lang.org/src/core/iter/range.rs.html#500-552 https://doc.rust-lang.org/src/core/iter/range.rs.html#500-55...