Skip to content

Panicking when an Option is unwrapped should give a useful error message #30429

Closed
@jimmycuadra

Description

@jimmycuadra

Given the Rust program:

fn main() {
    let option: Option<bool> = None;
    option.unwrap();
}

Running the program outputs:

thread '<main>' panicked at 'called `Option::unwrap()` on a `None` value', ../src/libcore/option.rs:365

The same program using expect instead of unwrap outputs:

thread '<main>' panicked at 'oops', ../src/libcore/option.rs:333

In a program of non-trivial size, figuring out which call to unwrap or expect generated the panic is harder than it needs to be, since the line shown is where the panic actually happens inside libcore. For improved ergonomics, the file and line reported should be from the user's source code where the unwrap or expect was called.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.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