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.
error[E0308]: mismatched types
--> src/main.rs:3:18
|
3 | let n: u32 = "42";
| --- ^^^^ expected `u32`, found `&str`
| |
| expected due to thisCommon 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.
let n: u32 = "42".parse()?; // &str -> u32
let s: &str = &owned_string; // &String -> &str
let x = value as u64; // numeric castRead the expected/found arrows
- The error underlines what was
expectedand what wasfound-- align them. - Check whether a reference, deref, or
.into()/.to_string()resolves it. - Fix the signature instead if the function should accept the found type.
How to prevent it
- Run
cargo checklocally to surface type errors before CI. - Prefer explicit conversions (
.into(),as,parse()) over guessing. - Let the compiler's expected/found notes guide the fix.