Email me

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

  • &str where a String is expected (or the reverse).
  • A trailing semicolon on the last line of a function, so it returns () instead of the value.
  • if and else branches that return different types.
  • Integer types that do not match, like u32 vs usize; Rust never converts numbers silently.
  • Returning T where the signature says Option<T> or Result<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

E0308Types and traits
Activein rustc 1.99.0
8related errors linked below

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

All 518 Rust error codes · Rust crate data · Glossary

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?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ