Sub topic 4
Fixing mismatched types
This chapter assumes you can write basic Rust functions and let bindings, run cargo check, and recognize the parts of a rustc diagnostic: the error code, the message, the primary span, and the help text.
What E0308 means
Rust checks that every expression has the type its context expects. When the two disagree, rustc reports E0308: mismatched types. The message shows an expected type and a found type. Your job is to decide which side is wrong: the code that produces the value, or the place that consumes it.
Rust does not convert between types silently. An i32 is never quietly turned into an f64, and a &str is never quietly turned into a String. This is why mismatches show up often early on.
Case 1: the stray semicolon
A function body ends with an expression, and its value is the return value. A trailing semicolon turns that expression into a statement, so the body produces ().
fn double(x: i32) -> i32 {
x * 2;
}
fn main() {
println!("{}", double(4));
}rustc reports expected i32, found (). It labels the function's return type as the source of the expectation, and the help text suggests removing the semicolon. Remove it:
fn double(x: i32) -> i32 {
x * 2
}
fn main() {
println!("{}", double(4));
}Case 2: the wrong type in an argument or binding
Suppose a function wants a String and you pass a &str. The diagnostic's primary span points at the argument. A note points at the function parameter that set the expectation.
fn shout(s: String) -> String {
s.to_uppercase()
}
fn main() {
println!("{}", shout("hello".to_string()));
}You can fix a mismatch in two directions:
- Change the value: convert it with
.to_string(),String::from,.into(), orasfor numeric casts. - Change the expectation: edit the parameter or the annotation, if the declared type was the mistake.
Choose by asking which side reflects what the program means. Don't add a conversion just to silence the error.
Case 3: mixing numeric types
Arithmetic needs both operands to have the same type. Adding an i32 to an f64 fails, but with E0277 rather than E0308, because the problem is a missing trait implementation (Add<f64> for i32). Chapter 10 covers trait bounds. The fix here is an explicit conversion:
fn main() {
let total: i32 = 5;
let extra: f64 = 1.5;
println!("{}", total as f64 + extra);
}Casting with as can lose data when you go from a wider type to a narrower one, for example f64 to i32 truncates. Cast toward the wider type when you can.
Reading expected and found
Common mistakes
- Treating
found ()as a mystery. It almost always means a trailing semicolon, or a block orifbranch that ends in a statement. - Fixing the first suggestion without reading the label. The help text is a guess. Confirm it matches your intent.
- Mismatched
ifandelsebranches. Both branches must produce the same type, so check each one.
Practice
Exercise
A function declared as fn area(w: u32, h: u32) -> u32 has the body w * h;. What error code does rustc report, what are the expected and found types, and what is the fix?
Show answer
It reports E0308 mismatched types. The expected type is u32 and the found type is (). The trailing semicolon makes the last line a statement, so the function returns nothing. Remove the semicolon so w * h is the return expression.
Exercise
let n: i32 = 7; let half: f64 = n / 2.0; fails to compile. Name two valid fixes.
Show answer
Convert the operand: let half: f64 = n as f64 / 2.0;. Or declare n as f64 from the start: let n: f64 = 7.0;. The first changes the value, the second changes the declared type.