I’m trying to figure why the following Rust snippet is allowed:
let mut a = &12;
let b = &&34;
a = b; // <- seems like &&i32 is coerced to &i32
This is a reduced version of Rust code I've seen in the wild. Here a has the inferred type &i32 and b has the inferred type &&i32. Yet, even though there not the same type, the assignment it allowed.
I can't find anything in the Rust reference explaining why this is legal. What I can find is:
Basically, any situation where the the compiler can figure out what the target type is, is a coercion site.
Assignment expressions as coercion sites feels a bit different to me compared to those documented in the reference.
In all those cases the coercion can be matched up with an explicit type annotation. For instance arguments are coercion sites and they can be matched to the parameter type annotation, let statements are only coercion sites when they have an annotation, etc.
For assignment expressions there may not be any annotation (like in my example) and yet a coercion can occur.