# Implement RangeBounds for &Range\*

**URL:** <https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704>\
**Category:** libs\
**Created:** [October 13, 2023, 8:54am UTC](https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704 "2023-10-13T08:54:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qsantos](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsantos/32/11413_2.png) [@qsantos](https://internals.rust-lang.org/u/qsantos)\
**Post date:** [October 13, 2023, 8:54am UTC](https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704/1 "2023-10-13T08:54:00Z")

</div>

I have encountered a situation very similar to [a Stack Overflow question](https://stackoverflow.com/questions/74056427/how-to-use-borrowborrow-to-accept-anything-that-borrows-as-a-trait). 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:

```rust
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:

```rust
    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:

```rust
fn f<T: Debug, U: Debug + RangeBounds<T>>(range: U) {
    println!("{range:?}");
}

```

Are there any gotchas that would make this a bad idea?

---

<div class="post-metadata">

**Author:** ![jdahlstrom](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jdahlstrom/32/3351_2.png) [@jdahlstrom](https://internals.rust-lang.org/u/jdahlstrom)\
**Post date:** [October 13, 2023, 12:44pm UTC](https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704/2 "2023-10-13T12:44:41Z")

</div>

I'd like to have this for another reason (and was slightly surprised it doesn't exist): if you want to reuse a range (eg. to index two slices in lockstep\[1\]) you (infamously) have to either resort to cloning or use two separate range literals:

```rust
fn foo(a: &[i32], b: &[i32]) {
    let r = compute_start()..compute_end();
    bar(a[r.clone()]);
    bar(b[r]);
    // Or
    let (start, end) = (compute_start(), compute_end());
    bar(a[start..end)]);
    bar(b[start..end]);
}
```

* * *

1. My real-world use case: the color and depth buffers in a 3D rasterizer

---

<div class="post-metadata">

**Author:** ![benschulz](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/benschulz/32/11346_2.png) [@benschulz](https://internals.rust-lang.org/u/benschulz)\
**Post date:** [October 13, 2023, 3:18pm UTC](https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704/3 "2023-10-13T15:18:06Z")

</div>

> [@qsantos](#):
>
> 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:

You can restrict the options that inference can consider:

> **[Rust Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8418e23e01f25b95687bae73b31d1e0f)**
>
> A browser interface to the Rust compiler to experiment with the language

---

<div class="post-metadata">

**Author:** ![qsantos](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/qsantos/32/11413_2.png) [@qsantos](https://internals.rust-lang.org/u/qsantos)\
**Post date:** [October 13, 2023, 4:59pm UTC](https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704/4 "2023-10-13T16:59:11Z")

</div>

Oh, that's clever, that would work in my case. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [January 11, 2024, 4:59pm UTC](https://internals.rust-lang.org/t/implement-rangebounds-for-range/19704/5 "2024-01-11T16:59:37Z")

</div>

Closed on January 11, 2024, 4:59pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
