Rust errors / Types and traits
Rust compiler error · checked with rustc 1.99.0
Rust error E0308: mismatched types
What it meansA value has a different type from the one the code around it needs. The message says which type was expected and which one was found, and the arrow points at the culprit.
What rustc prints
error[E0308]: mismatched types --> main.rs:7:10 | 7 | plus_one("Not a number"); | -------- ^^^^^^^^^^^^^^ expected `i32`, found `&str` | | | arguments to this function are incorrect | note: function defined here --> main.rs:3:4 | 3 | fn plus_one(x: i32) -> i32 { | ^^^^^^^^ ------
Real output of rustc 1.99.0 compiling the official example below (2021 edition). Your names and line numbers will differ; the first line is the part to match.
Why it happens
&strwhere aStringis expected (or the reverse).- A trailing semicolon on the last line of a function, so it returns
()instead of the value. ifandelsebranches that return different types.- Integer types that do not match, like
u32vsusize; Rust never converts numbers silently. - Returning
Twhere the signature saysOption<T>orResult<T, E>.
How to fix it
1. Convert between String and &str
.to_string() or String::from one way, &s or .as_str() the other.
let name: String = "Ada".to_string();
let r: &str = &name;2. Drop the semicolon on the return value
The last expression without ; is the return value.
fn double(x: i32) -> i32 {
x * 2 // no semicolon
}3. Convert numbers explicitly
as for known-safe casts, try_from when it might not fit.
let n: usize = v.len();
let small: u32 = u32::try_from(n).unwrap_or(u32::MAX);4. Wrap the value
Return Some(x) or Ok(x) to match the signature.
fn find(id: u32) -> Option<User> {
let u = load(id);
Some(u)
}Other wordings of this error
mismatched types, expected `, found `, expected due to this, expected struct `String`, found `&str`, expected `()`, found
The compiler team's explanation
Expected type did not match the received type.
Erroneous code examples:
fn plus_one(x: i32) -> i32 {
x + 1
}
plus_one("Not a number");
// ^^^^^^^^^^^^^^ expected `i32`, found `&str`
if "Not a bool" {
// ^^^^^^^^^^^^ expected `bool`, found `&str`
}
let x: f32 = "Not a float";
// --- ^^^^^^^^^^^^^ expected `f32`, found `&str`
// |
// expected due to this
This error occurs when an expression was used in a place where the compiler expected an expression of a different type. It can occur in several cases, the most common being when calling a function and passing an argument which has a different type than the matching type in the function declaration.
From rustc --explain E0308 (1.99.0), part of the Rust project, MIT or Apache-2.0. Official page.
Related errors
- E0277the trait bound is not satisfied
- E0599no method named X found in the current scope
- E0433failed to resolve
- E0282type annotations needed
- E0284type annotations needed
- E0307invalid `self` parameter type: `&Bar`
- E0321cross-crate traits with a default impl, like `Send`, can only be implemented for a struct/enum type, not `&'static Foo`
- E0322explicit impls for the `Sized` trait are not permitted
Up next
Often hit nextE0277: the trait bound is not satisfiedA related error from the same family.Read it next →Did this answer your question?