Title
copied successfully!
How does auto-coercion work
This example is contributed by Bart Massey.
Why does this code compile when the type of x is not right?
fn f(x: &u32) {
println!("{}", x);
}

fn main() {
f(&&5);
}
Without the code in question, it's hard to give a specific answer to your question. However, I can tell you that type inference and coercion might explain why some code compiles in Rust even if you think the type of variable is not correct. In Rust, the compiler often can infer the type of a variable from the context, or auto-coerce the type to another type if required. If you initialize a variable with a certain value, the compiler will automatically infer the type:
let x = 5; // x is inferred to be i32
In some cases, Rust will also coerce types for you. An example of this is working with references:
let s = "hello"; // s is inferred to be &str
let t: &str = &s; // You can assign s to t even though t is a reference to a &str
Even though s is a &str and t is a &&str, this code still compiles because Rust will automatically dereference &s to s to match the type. If you believe there may be a type problem with the code, and it's still compiling, this could be one of the reasons. However, I would need to see the specific code to give a concrete answer.