Can someone walk me through some implicit type conversions?

Hey all, I ran into a case recently where I had a Rust function that wanted a slice reference, but I had a Vec, and just to be cheeky, I tried dereferencing the Vec and the function accepted it just fine:

// The function
fn update(buffer: &[u32]) { ... }

// My object
let mut buffer: Vec<u32> = ...;

// Nice, it just works :)
update(&buffer);

This is rad, but I'm a bit confused at how this actually works. What specific types/traits/functions/etc were involved to get from type &Vec<T> to &[u32] ?

1 Like

See Deref coercions.

Neat :slight_smile: Thanks for the link.

Aha, thanks! Just what I needed. Indeed, looking at Vec's source, there's a Deref impl with target type [T] that's doing exactly this. Awesome :slight_smile:

https://github.com/rust-lang/rust/blob/master/src/libcollections/vec.rs#L1218

1 Like