Borrow a Vec or use .as_slice() when passing to a &[T]

Hi,

Say you have this function:

fn do_with_slice(items: &[i32]) {
    for i in items {
        println!("{}", i);
    }
}

And a Vec of i32:

let v = vec![1, 2, 3];

Is it more ideomatic and/or optimal to use do_with_slice(&v); or do_with_slice(v.as_slice()); when calling the function?

Thanks!

I more often see &v.

3 Likes

As far as I know, the whole point of deref coercions is to allow simply writing &v in such situations, for example.

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.