rust-lang / rust-lang/rust

Suggest changing `main`s return type on E0277 (`?` / `from_residual`)

Open
#125,997 2 comments 3 reactions 1 assignee View on GitHub

@surechen is already working on this.

Since Jun 7, 2024.

A-diagnostics C-enhancement T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
use std::fs::File;
use std::io::prelude::*;

fn main() {
    let mut file = File::create("foo.txt")?;
    file.write_all(b"Hello, world!")?;
}
Current output
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that
implements `FromResidual`)
 --> cargo-14007.rs:5:43
  |
4 | fn main() {
  | --------- this function should return `Result` or `Option` to accept `?`
5 |     let mut file = File::create("foo.txt")?;
  |                                           ^ cannot use the `?` operator in a function that returns `()`
  |
  = help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`

error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that
implements `FromResidual`)
 --> cargo-14007.rs:6:37
  |
4 | fn main() {
  | --------- this function should return `Result` or `Option` to accept `?`
5 |     let mut file = File::create("foo.txt")?;
6 |     file.write_all(b"Hello, world!")?;
  |                                     ^ cannot use the `?` operator in a function that returns `()`
  |
  = help: the trait `FromResidual<Result<Infallible, std::io::Error>>` is not implemented for `()`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `cargo-14007` (bin "cargo-14007") due to 2 previous errors
Desired output

To suggest changing

fn main() {
    // ...
}

to

fn main() -> Result<(), Box<dyn Error>> {
    // ...
    Ok(())
}
Rationale and extra context

In rust-lang/cargo#14007, concern was raised over people generating a trivial fn main() {} with cargo new and copy/pasting example code that uses ? (which is encouraged in API guidelines). Regardless of the decision made for cargo new, this applies just as well to users hand writing a trivial fn main() {}. These are likely new users who would be intimidated by the current message and wouldn't know what next steps to take

Other cases

No response

Rust Version
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.