Skip to content
Latchkey

Rust "error[E0308]: mismatched types" in CI

The compiler expected one type and got another. A value, return, or argument has a type that does not match what the context requires -- a real type error, identical on every run.

What this error means

cargo build fails with error[E0308]: mismatched types and an expected ... found ... pair pointing at the offending expression. The program does not compile.

cargo
error[E0308]: mismatched types
 --> src/main.rs:3:18
  |
3 |     let n: u32 = "42";
  |            ---   ^^^^ expected `u32`, found `&str`
  |            |
  |            expected due to this

Common causes

Wrong value type for the context

A literal, variable, or expression has a type that does not match the annotation, function signature, or expected return type at that point.

Missing conversion or deref

You passed &String where &str was wanted, an owned value where a reference was expected, or an integer of the wrong width -- a conversion is needed.

How to fix it

Convert to the expected type

Parse, cast, or convert the value to match what the context requires.

Rust
let n: u32 = "42".parse()?;   // &str -> u32
let s: &str = &owned_string;  // &String -> &str
let x = value as u64;         // numeric cast

Read the expected/found arrows

  1. The error underlines what was expected and what was found -- align them.
  2. Check whether a reference, deref, or .into()/.to_string() resolves it.
  3. Fix the signature instead if the function should accept the found type.

How to prevent it

  • Run cargo check locally to surface type errors before CI.
  • Prefer explicit conversions (.into(), as, parse()) over guessing.
  • Let the compiler's expected/found notes guide the fix.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →