I have an issue concerning the way references behave in rust : I made an example to show my misunderstanding, of course there is a much simpler version of the function, this is not the point, the point is the behaviour of references in Rust.
Consider these five following functions :
This code compile perfectly
fn add2(parameter : &isize) -> isize {
let a : isize = parameter + 2;
a
}
This code don't compile : mismatched type, expected isize, found &isize
fn add2(parameter : &isize) -> isize {
let a : isize = parameter;
a + 2
}
This code don't compile either : casting &isize as isize is invalid cannot cast &isize as isize (well, I expected it here)
fn add2(parameter : &isize) -> isize {
let a : isize = parameter as isize;
a + 2
}
This code compile
fn add2(parameter : &isize) -> isize {
let a : isize = *parameter;
a + 2
}
And finally, this code compile too
fn add2(parameter : &isize) -> isize {
let a : isize = *parameter + 2;
a
}
So my questions are :
- Why is the first one compiling if the second one does not ? (operator+ overload for references ?)
- Why auto-dereference mecanism doesn't work on the second one ? (and on the third one)