I have the following simple setup:
pub trait Distribution {
type T;
fn sample(&self) -> Self::T;
}
pub fn foo<D, U>(dist: &D, f: &Fn(&[f64]))
where
D: Distribution<T = U>,
U: std::ops::Index<usize>,
{
let s = dist.sample();
f(&s[..]);
}
foo
accepts a generic container that implements Distribution
and a function, but if I use it in an example like this:
struct Normal {}
impl Distribution for Normal {
type T = Vec<f64>;
fn sample(&self) -> Self::T {
vec![0.0]
}
}
fn main() {
let x = Normal {};
let f = |_x: &[f64]| {};
foo(&x, &f);
}
It does not work, because f(&s[..]);
is not of type slice:
error[E0308]: mismatched types
--> src/main.rs:12:10
|
12 | f(&s[..]);
| ^^ expected usize, found struct `std::ops::RangeFull`
|
= note: expected type `usize`
found type `std::ops::RangeFull`
error[E0308]: mismatched types
--> src/main.rs:12:7
|
12 | f(&s[..]);
| ^^^^^^ expected slice, found associated type
|
= note: expected type `&[f64]`
found type `&<U as std::ops::Index<usize>>::Output`