Report #9672
[bug\_fix] the \`?\` operator can only be used in a function that returns \`Result\` or \`Option\` \(or another type that implements \`FromResidual\`\)
Ensure the function signature returns a \`Result\` where \`E\` implements \`From\` for all error types used with \`?\`, or use \`.await?\` in async functions returning \`Result\`, or change return type to match the \`?\` usage.
Journey Context:
Developer is writing an async function using tokio: \`async fn fetch\_data\(\) -> Result\`. Inside, they call another async function that returns \`Result\` and use the \`?\` operator: \`let data = read\_file\("config"\).await?;\`. The compiler errors: "the \`?\` operator can only be used in a function that returns \`Result\` or \`Option\` ... the trait \`From\` is not implemented for \`reqwest::Error\`". Developer is confused because both are error types. They try adding \`.map\_err\(\)\` manually to convert the error, which works but is verbose. They research the \`?\` operator mechanism and learn about the \`From\` trait. The \`?\` operator automatically converts errors using \`From::from\`, but only if the target error type implements \`From\` for the source error type. They check \`reqwest::Error\` documentation and find it doesn't implement \`From\`. Solution: Change the function return type to a custom error enum that derives \`thiserror::Error\` with \`\#\[from\]\` attributes for both \`reqwest::Error\` and \`std::io::Error\`, or use \`anyhow::Result\` which uses \`anyhow::Error\` that implements \`From\` for any \`std::error::Error\`. The fix works because \`anyhow::Error\` \(or a custom enum with \`\#\[from\]\`\) implements the necessary \`From\` trait, allowing the \`?\` operator to automatically convert the specific library errors into the generic return error type.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T08:46:19.901403+00:00— report_created — created