Minimum code:
fn foo() {
let vector = vec![1u8, 2u8];
let a = &vector.as_slice()[0];
drop(vector);
let b = a;
}
fn bar() {
let array = [1u8, 2u8];
let a = &array[0];
drop(array);
let b = a;
}
When compiling the above codes, foo
can't compile while bar
can.
In function foo
, a
borrows the vector
, so after I drop vector
, I can't access a
any more.
However, I think the situation is same for bar
, but bar
can successfully pass the compilation. I don't know why.