trait Foo { }
fn set_element(
slice: &mut [impl Foo], val: impl Foo, index: usize
) {
slice[index] = val;
}
This does not compile; The error is as so:
error[E0308]: mismatched types
--> src/main.rs:10:20
|
8 | slice: &mut [impl Foo], val: impl Foo, index: usize
| -------- -------- found type parameter
| |
| expected type parameter
9 | ) {
10 | slice[index] = val;
| ------------ ^^^ expected type parameter `impl Foo`, found a different type parameter `impl Foo`
| |
| expected due to the type of this binding
|
= note: expected type parameter `impl Foo` (type parameter `impl Foo`)
found type parameter `impl Foo` (type parameter `impl Foo`)
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
impl Trait, I heard that these expect "very specific types" but that they don't display them, or that those aformentioned "very specific types" go undefined in the code.
So I can't set the value of the element to val, because the slice contains a different type than that of val?
I've tried to allow this to work via type coercion too, and it still fails.
My use case was that I wanted a buffer of futures that I would poll, and then when any one future finished, I was going to filter in more by replacing finished values in the buffer with newly started futures.
My question is whether this is correct, that setting values in a slice of impl Trait
is not something you can do? It feels like I have answered it myself, but I can't accept it, so here I am.