Improve design of assert_len by dylni · Pull Request #81154 · rust-lang/rust (original) (raw)

Maybe even just Range::subset_of(bounds, ..len)?

This is a good option, but it might distract from what this method's meant to do. It might be better to remove subset completely, since this method's only purpose is to convert RangeBounds to Range.

I would have expected the name to become something more like Range::from_range, as it is now a constructor.

I agree with @ExpHP about naming this method as a constructor. What does everyone think about Range::from_bounds(bounds, len)? Whenever I use this method, the length argument is obviously a length, so making it a range shouldn't be necessary. Now that there's another range argument, it can be confusing.

That method can return Result, or that can be done with checked_from_bounds or try_from_bounds. It might be better to not panic by default.