E0308: mismatched types
Rust does no implicit numeric or type conversion. An `i32` is not a `u32`, and a `String` is not a `&str`. Every conversion is written out in the source.
This looks strict next to JavaScript or Python, but it is why a Rust program that compiles usually behaves the way you expected on the first run.
Code that triggers it
fn takes_string(value: String) {
println!("{value}");
}
fn main() {
takes_string("a literal"); // error[E0308]: expected String, found &str
}Code that compiles
fn takes_string(value: String) {
println!("{value}");
}
fn main() {
takes_string("a literal".to_string());
}Convert explicitly: `.to_string()` or `String::from(...)` for text, `as` or `try_into()` for numbers. Or relax the parameter to `&str` if the function only reads it.
Try both versions
The broken version is loaded below so you can see the real compiler message, then fix it yourself.
Contrast AAA · 16.6:1
15px