Rust "?" Operator E0277 - Error Type Not Convertible in CI
You used ? to propagate an error, but the inner error type does not implement From into the function's return error type. The ? operator needs that conversion, and without it the build fails with E0277.
What this error means
cargo build fails with error[E0277]: ? cannot convert the error, noting the trait From<X> is not implemented for Y` at the ? site. Common when a function returns a custom error but ?` yields a std/library error.
error[E0277]: `?` couldn't convert the error to `AppError`
--> src/main.rs:6:30
|
6 | let f = std::fs::read("x")?;
| ^ the trait `From<std::io::Error>` is not
| implemented for `AppError`Common causes
No From impl from the inner error
The function returns Result<_, AppError>, but the ?-propagated error is a different type with no From<ThatError> for AppError, so the conversion fails.
Mixing concrete and boxed error types
Using ? across functions whose error types do not line up (concrete vs Box<dyn Error> vs a custom enum) leaves no conversion path.
How to fix it
Implement From for the conversion
Provide From<InnerError> for your error type so ? can convert.
impl From<std::io::Error> for AppError {
fn from(e: std::io::Error) -> Self { AppError::Io(e) }
}Or use a derive/boxed error
thiserror derives From for you; anyhow boxes any error so ? always converts.
#[derive(thiserror::Error, Debug)]
enum AppError {
#[error("io")] Io(#[from] std::io::Error),
}
// or: fn run() -> anyhow::Result<()> { ... ? ... }How to prevent it
- Derive error types with thiserror (with
#[from]) so conversions exist. - Use anyhow at application boundaries where any error should propagate.
- Keep return error types consistent across the call chain.