Skip to content

Rust Guide: incorrect explanation of Result.ok() / Option.expect() #17676

Closed
@tcard

Description

@tcard

Rust provides a method on these IoResults called ok(), which does the same thing as our match statement, but assuming that we have a valid value. If we don't, it will terminate our program. In this case, if we can't get input, our program doesn't work, so we're okay with that. In most cases, we would want to handle the error case explicitly. The result of ok() has a method, expect(), which allows us to give an error message if this crash happens.

If I'm not mistaken, ok() just turns the IoResult into an Option. It won't terminate the program. It's expect() which fails or unwraps the value.

Also, wouldn't a call to the IoResult's unwrap() be more direct? Although the custom failure message would be lost.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions