I'm relatively new to Rust but have been reading the The Book for quite some time and just starting to get into some personal projects to learn the language. What's confusing me slightly is references, specifically the cases where I am required to explicitly dereference them. I have read in the book about the Deref trait and also about Deref Coercion etc and I understand these for the most part but what's confusing me is more simple operations on references like adding two i32
referencing for example.
fn main() {
let a = 10;
let b = 5;
let c = add(&a, &b);
println!("c = {}", c);
}
fn add(a: &i32, b: &i32) -> i32
{
a + b
}
The code above compiles and runs perfectly but I am wondering why it worked without explicitly dereferencing the the function parameters (operator overloading maybe ?). In general what I am asking is if someone could explain to me the cases in which rust will automatically dereference for me.