Can someone explain why this compiles:
fn main() {
let a = vec![1, 2, 3];
println!(\"{:?}\", a[4]);
}
When running it, I got:
Maybe what you mean is :
fn main() {
let a = vec![1, 2, 3];
println!("{:?}", a[4]);
}
This returns an Option
so it will return Some
or None
. Compare this to:
fn main() {
let a = vec![1, 2, 3];
println!("{:?}", &a[4]);
}
This accesses by reference so it directly accesses the address
and causes the panic in your program.