A function that takes a slice of tuples of slices with the IntoIterator tag

I have a function fthat needs to be called like this:

struct S;

let s = &S;

f(&[(s, &[0.1, 0.2, 0.3]), (s, &[0.5, 0.6])]);

for a convenient interface, I want to fprocess any type IntoIterator. At the moment, my signature

fn f<'a, I, J: 'a>(iter: I)
where
    I: IntoIterator<Item = &'a (&'a S, J)>,
    J: IntoIterator<Item = &'a f32>

Unfortunately, the compiler treats slices as arrays of fixed size, so I got this error:

error[E0308]: mismatched types
   --> sandbox/src/main.rs:142:33
    |
142 |     f(&[(s, &[0.1, 0.2, 0.3]), (s, &[0.5, 0.6])]);
    |                                    ^^^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
    |
    = note: expected type `&[{float}; 3]`
               found type `&[{float}; 2]`

replacing Jwith Vec<f32>works, but that’s not how I want it.

First question: is there a way to handle this, that I can name the function as I mentioned? At first I thought that this can be solved with the help of a Grade of a higher rank , but they are available only for the parameters of life.

: , , IntoIterator? J::into_iter()?

+4
1

, , . :

41 |     let x = &[(s, &[0.1, 0.2, 0.3]), (s, &[0.5, 0.6])];
   |                                          ^^^^^^^^^^^ expected an array with a fixed size of 3 elements, found one with 2 elements
   |
   = note: expected type `&[{float}; 3]`
              found type `&[{float}; 2]`

, (&S, &[{float}; 3]). , 2. , / &[{float}].

, :

f(&[(s, &[0.1f32, 0.2, 0.3] as &[f32]), (s, &[0.5, 0.6])]);

, IntoIterator:

f(&[(s, [0.1, 0.2, 0.3].iter()), (s, [0.5, 0.6].iter())]);

, ( ). , , :

fn f2<T>(iter: &[(&S, &[T])])
{
}

f2(&[(s, &[0.1, 0.2, 0.3]), (s, &[0.5, 0.6])]);

, : , , , , .

+4

Source: https://habr.com/ru/post/1694128/


All Articles