E0308 🦀 Rust

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

  1. Convert explicitly (.to_string(), as, try_into)
  2. Handle Option/Result with ?, unwrap_or, match
  3. Remove the trailing semicolon if the block should return a value

Detailed fix by platform

Rust

  1. rust
    fn parse(s: &str) -> Result<u32, std::num::ParseIntError> {
        let n: u32 = s.trim().parse()?;
        Ok(n)
    }

How to diagnose

  1. Expected/found — Types in the message
  2. 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:

🧠 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.