Rust: error[E0308]: mismatched types — expected `X`, found `Y`
A value’s type doesn’t match what the context expects — common with &str vs String, Option/Result vs plain values, and integer types.
Seen on:
Rust
Meaning
Rust doesn’t convert types implicitly. Use .to_string()/&s, unwrap/? for Option and Result, as or try_into for numbers, and check what an if/match branch or function returns.
Common causes
- &str vs String
- Option<T>/Result<T,E> where T expected
- Different integer types (usize vs i32)
- Missing or extra semicolon changing a block’s return type to ()
⚡ Quick fix
- Convert explicitly (.to_string(), as, try_into)
- Handle Option/Result with ?, unwrap_or, match
- Remove the trailing semicolon if the block should return a value
Detailed fix by platform
Rust
- rust
fn parse(s: &str) -> Result<u32, std::num::ParseIntError> { let n: u32 = s.trim().parse()?; Ok(n) }
How to diagnose
- Expected/found — Types in the message
- Semicolon — Block ends with ;?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Other ways to find it
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 7 Oct 2026