Correct way to pass vector of vector references

I have a function that requires of me to pass my 2d vector as &[&[u8]], But it seams i cannot do that. Example:

fn flip(vc:&[u8])->Vec<u8>{
    let mut vec = vc.to_vec();
    let l = vec.len();
    let tmp = vec[0];
    vec[0] = vec[l-1];
    vec[l-1] = tmp;

fn pass (v:&[&[u8]]){

fn main() {
    let mut vec= Vec::new();
    let mut vc = vec![1u8,2,3,4,5];
    for _i in 0..3 {
        vc = flip(&vc);


   Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
  --> src/
26 |     pass(&*vec);
   |     ---- ^^^^^ expected slice `[u8]`, found struct `Vec`
   |     |
   |     arguments to this function are incorrect
   = note: expected reference `&[&[u8]]`
              found reference `&[&Vec<u8>]`
note: function defined here
  --> src/
11 | fn pass (v:&[&[u8]]){
   |    ^^^^  ----------

For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error

Given i cannot change fn pass(), is there a was to resolve this issue?

If you can't change pass no. Or you need to collect references to your Vecs in another Vec and pass that. &[T] and Vec<T> don't have the same layout, so Vec<Vec<T>> cannot be trivially passed as &[&[T]].

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.