2

I have a vector of i32. I have created 2 functions

  1. fn1 which accepts a reference to an array of i32
  2. fn2 which accepts a reference to a vector of i32

I am not able to understand why does this code work for fn1. Shouldn't the compiler throw mismatched types error because we are passing reference to a vector whereas fn1 expects a reference to an array?

fn main() {
    let my_vec = vec![1,2,3];
    fn1(&my_vec);
    fn2(&my_vec);
    
}

fn fn1(number_vec: &[i32]){
    dbg!(number_vec);
}


fn fn2(number_vec: &Vec<i32>){
    dbg!(number_vec);
}

I feel rust must be changing the type automatically. If this is the case please explain how its being done.

Kanwar Baweja
  • 183
  • 1
  • 11

0 Answers0