E0308
Rust Programming Language
Severity: MinorWhat Does This Error Mean?
Rust error E0308 means you gave a function or variable a value of the wrong type. Rust checks types at compile time and refuses to continue if they do not match. For example, passing a String where an integer is expected will cause this error. Fix it by converting the value to the correct type or updating the expected type.
Affected Models
- Rust 1.0 and later
- All Rust editions (2015, 2018, 2021)
- cargo build and rustc
Common Causes
- Passing a value of one type to a function that expects a different type
- Returning a value from a function that does not match the declared return type
- Assigning a value to a variable that was already inferred as a different type
- Forgetting to convert a String to a &str or vice versa
- Using an integer literal where a float or custom type was expected
How to Fix It
-
Read the compiler error message carefully. It will say something like 'expected &str, found String'. Note both types.
The error shows the expected type and the found type. This tells you exactly what conversion you need.
-
If you have a String but need a &str, call .as_str() or pass &my_string instead of my_string.
This is one of the most common causes. String and &str are related but different types in Rust.
-
If you have an integer but need a float, add a decimal: write 5.0 instead of 5, or use 5 as f64 to cast it.
Rust does not automatically convert numbers between types the way some other languages do.
-
If the error is in a function return, make sure the last expression in the function matches the return type declared after ->.
Remember: in Rust, the last line without a semicolon is the return value. Adding a semicolon turns it into a statement that returns ().
-
If you cannot change the value, change the function signature to accept the type you actually have.
Sometimes the cleanest fix is to update the parameter type rather than convert every call site.
When to Call a Professional
E0308 is always a compile-time error — your program will not run until it is fixed. The Rust compiler message usually tells you exactly what type was expected and what type was found. Read the full error output carefully. It almost always points directly to the problem line.
Frequently Asked Questions
Why does Rust not just convert the types automatically?
Rust's strict type system is intentional. Automatic type conversion can hide bugs that are hard to find later. By forcing you to convert explicitly, Rust makes sure you always know exactly what type you are working with. This strictness is one reason Rust programs tend to be very reliable once they compile.
The error says 'expected (), found integer' — what does that mean?
It means your function is declared to return nothing (unit type, written as ()) but you have a value on the last line. This usually happens when you accidentally add a semicolon to the return expression. Remove the semicolon from the last line of your function to return the value properly.
How do I convert between common types to fix E0308?
Integer to float: use 'x as f64'. String to &str: use '&my_string' or 'my_string.as_str()'. &str to String: use 'my_str.to_string()' or 'String::from(my_str)'. The Rust docs have a full list of conversions for every type combination.