Dereferencing has no effect


#1

Hi,

Can someone please explain to me why the following two apparently different pieces of code are compiling successfully and giving the same result – with or without dereferencing?

fn disp(s: &mut Vec<i32>) {

	s[1] = s[1] * 2;

	println!("{:?}", s[1]);
}

fn main() {

	let mut s = vec![1, 2, 3];
   	
   	disp(&mut s);
}

and

fn disp(s: &mut Vec<i32>) {

	(*s)[1] = (*s)[1] * 2;

	println!("{:?}", s[1]);
}

fn main() {

	let mut s = vec![1, 2, 3];
   	
   	disp(&mut s);
}

Thanks


#2

Read about Deref coercions here: https://doc.rust-lang.org/book/deref-coercions.html


#3

Thanks!


#4

In this case, it’s a straight Deref of a vec to a slice - I don’t even think there’s any coercion happening.