I'm trying to learn Rust. While playing around with references, I noticed that Rust apparently automatically dereferences primitive types.
fn takes_ref(bytes: &[u8; 2]) {
println!("str[0]: {}", bytes[0]); // <-- bytes[0] compiles even though it's a ref.
}
fn takes_ref_correct(bytes: &[u8; 2]) {
println!("str[0]: {}", (*bytes)[0]); // This is correct, first I dereference bytes and then access it's contents
}
fn takes_ref_to_ref(bytes: &&[u8; 2]) { // takes reference to reference
println!("str[0]: {}", bytes[0]); // this works as well
}
All these functions compile and behave in the exact same way. Why? Does the [] operator automatically dereferences refs? Do any other operators behave like this?