error[E0308]: mismatched types
A value has a different type from the one the code expects. The expected X, found Y part of the message names both types.
Quick fix: Read the expected and found types, then convert explicitly or fix the expression. A stray semicolon is the most common cause.
Code that triggers E0308
fn add(a: i32, b: i32) -> i32 {
a + b;
}
fn main() {
println!("{}", add(2, 3));
}What the compiler says
error[E0308]: mismatched types
--> src/main.rs:1:27
|
1 | fn add(a: i32, b: i32) -> i32 {
| --- ^^^ expected `i32`, found `()`
| |
| implicitly returns `()` as its body has no tail or `return` expression
2 | a + b;
| - help: remove this semicolon to return this value
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.Why Rust raises E0308
E0308 is the most common Rust error, and its message tells you exactly which two types disagree.
In this example the culprit is a single character: the semicolon after a + b. In Rust, the last expression in a block, written without a semicolon, is the block’s value. With the semicolon, a + b; becomes a statement, the function body evaluates to () (the unit type), and () isn’t the i32 the signature promises.
Two other causes you’ll meet early on: Rust never converts between number types for you, and a string literal is a &str, not a String.
Dividing two u32 values gives a u32, not the f64 the function returns:
fn average(total: u32, count: u32) -> f64 {
total / count
}
fn main() {
println!("{}", average(17, 4));
}error[E0308]: mismatched types
--> src/main.rs:2:5
|
1 | fn average(total: u32, count: u32) -> f64 {
| --- expected `f64` because of return type
2 | total / count
| ^^^^^^^^^^^^^ expected `f64`, found `u32`
|
help: you can convert a `u32` to an `f64`, producing the floating point representation of the integer
|
2 | (total / count).into()
| + ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.A string literal is a &str, and a String variable needs an owned string:
fn main() {
let name: String = "Ferris";
println!("{name}");
}error[E0308]: mismatched types
--> src/main.rs:2:24
|
2 | let name: String = "Ferris";
| ------ ^^^^^^^^- help: try using a conversion method: `.to_string()`
| | |
| | expected `String`, found `&str`
| expected due to this
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0308`.How to fix E0308
1. Remove the semicolon
Make a + b the tail expression of the function. It becomes the return value. Writing return a + b; works too.
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
println!("{}", add(2, 3));
}2. Convert explicitly
Rust never converts types implicitly. Use f64::from (or as f64) for numbers, and .to_string() or String::from to turn a &str into a String.
fn average(total: u32, count: u32) -> f64 {
f64::from(total) / f64::from(count)
}
fn main() {
let name: String = "Ferris".to_string();
println!("{name}: {}", average(17, 4));
}Related errors
- E0277: type doesn’t implement
std::fmt::Display - E0369: binary operation
==cannot be applied to type - E0599: no method named
push_strfound for reference&str
Learn the concept behind it
The Functions lesson explains the rules behind E0308 with diagrams and more examples. For the official reference, run rustc --explain E0308 or read the E0308 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.