Skip to content

Support checking whether std::process::ExitCode represents success or failure #113081

Open
@nya3jp

Description

@nya3jp

IIUC there is no good way to check whether a std::process::ExitCode corresponds to success or failure. This is cumbersome, for example, when we want to check the exit code just before actually exiting.

use std::process::ExitCode;

fn real_main() -> ExitCode {
    // ... do something ...
    ExitCode::FAILED
}

fn main() -> ExitCode {
    let exit_code = real_main();
    
    // For now, the only way to check if `exit_code` is success is to check its
    // Debug serialization:
    if format!("{:?}", exit_code) != "ExitCode(unix_exit_status(0))" {
        eprintln!("FAILED");
    }
    
    // Ideally we want to write this like:
    /*
    if !exit_code.is_success() {
        eprintln!("FAILED!");
    }
    */

    exit_code
}

Can we add methods to std::process::ExitCode checking whether it is success or failure?

It seems like is_success/is_failure were considered when stabilizing std::process::ExitCode, but it was left as an unresolved question. #48711

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-processArea: `std::process` and `std::env`T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions