Documents tell me vector can be indexed because it has implemented Index trait;
But there is something I confused, like the code following:
use std::ops::Index;
fn main() {
let mut v = Vec::with_capacity(10);
v.push(0);
// can't compare `{integer}` with `&{integer}`
// assert_eq!(v[0], Index::index(&v, 0));
let v0 = unsafe { &*(&v[0] as *const _) };
v[0] = 9;
let _val = *v0;
println!("{}", _val);
}
- v[0] is a type of integer, but call the index directly returns a type of &integer
- Then v[0] is a integer, OK, it maybe a temp variable.
But the output of println is 9, means v[0] is not passed by value, it is not a integer.
It seems to me &v[0]
is a special syntax that can't been split into &
and v[0]
Is here some magic?