I'm writing a program that needs to handle numbers within a specific range. The program itself isn't that important (FWIW, it's calculating "Square loops"), but I want to make it as general as I can, in order to be able to use it for variations on the original problem.
I'm struggling to find the best way to express the type of one of my arguments. Logically, it's a collection of values (i32, typically), and all I ever do with it is extract the "first" value, and test a value for membership.
At the moment, I'm using a RangeBounds<T> as my parameter type, because the typical usage is to pass a range like 1..10. The .contains method is efficient (it's just 2 comparisons), and I can get the first value via .start_bound() (with some juggling to catch ranges with no lower bound). But this limits me to only passing ranges. An alternative would be to declare the type as Iterator<T>, and use .first() and .contains(). If I do that, I'd pass (1..10).into_iter(), which is fine, but my concern is whether that would still use the efficient .contains() implementation that a Range object can support.
To some extent, this doesn't matter - for my current use, it's fast enough either way. But I'm interested in learning how to write good Rust code, not just solve my current problem. And I don't know how I can find out the answer to this question. Any pointers would be very helpful ![]()