Hi,
I started learning Rust last week and so far I had a great experience using it. When building sandbox examples I stumbled over a peculiar thing, though:
When defining a function:
fn foo(n : &i32) -> i32 {
return *n;
}
I can then go on and call it with:
let mut n = 5;
let ret = foo(&mut n);
without the compiler complaining.
So my question is: why is this not a type mismatch compiler error? (&mut vs &)
I understand that this is a constructed case and that the function can safely operate on the passed mutable reference. It just feels inconsistent to me, am I missing something?
PS: for another example see this repo (rust-clippy)
I guess because & T and &mut T are different types strictly speaking. (This gets related to the discussion we were having on the thread where I originally thought a borrow implicitly matched a trait bound of whatever it was borrowed)
If you are developing a trait Foo, think over immediately if it needs blanket implementations for Box<F>, &F and &mut F where F: Foo. Adding any of those blanket implementations is a breaking change.