I have encountered a situation very similar to a Stack Overflow question. Essentially, I want to write a method that accepts either an owned or borrowed range as an argument, to avoid users from having to write f(&(1..32))
. Naturally, we can use Borrow to do this:
fn f<T: Debug, U: Debug + RangeBounds<T>, V: Borrow<U>>(range: V) {
let range = range.borrow();
println!("{range:?}");
}
However, the compiler cannot infer the type of U since it could be anything, not just the dereferenced argument. Thus, when calling it with a borrowed range, we need to provide the type:
let range = 1..2;
f::<_, Range<u32>, _>(&range);
If &Range<T>
, and others were to implement RangeBound
, the problem would become moot, since one could just write:
fn f<T: Debug, U: Debug + RangeBounds<T>>(range: U) {
println!("{range:?}");
}
Are there any gotchas that would make this a bad idea?