Consider the following code:
fn fun() {
let mut vec = vec![];
{
let x: &[u8] = &[1, 2];
vec.push(x); // why is the compiler happy?
let y = [3, 4];
let z: &[u8] = &y;
vec.push(z); // ok compiler is not happy
}
println!("{:?}", vec); // after commenting this line the compiler is happy again
}
I understand why the compiler complains about "borrowed value does not live long enough"
for y
but why is it happy with x
? x
is a reference to a slice without a name -- when is this anonymous slice dropped and what is its lifetime?
After commenting the println
everything compiles, I guess this is because the compiler understands that vec
is not used anywhere so it does not care.