Expected struct `std::process::ExitStatus`, found `()`

So I am trying to write a piece of Rust code that would open a file in one location and, if unsuccessful, open it in another.

let f =open::that("assets/maps.png");
match f {
    Ok(file) => file,
    Err(error) => {
        open::that("assets/map/maps.png").unwrap();
    },
};

This caused the error: expected struct `std::process::ExitStatus`, found `()
I am new to this language, so I am not sure how to handle this problem, so if you can, please help. Thank you.

Try removing the semicolon inside the match expression, so both of its arms evaluate to the same type (ExitStatus).

Or, since you aren't using the result of the match expression, you can keep the semicolon, and remove the unused value from the first arm:

let f = open::that("assets/maps.png");
match f {
    Ok(_) => {}
    Err(_) => {
        open::that("assets/map/maps.png").unwrap();
    }
}

And since your match has only one case that contains any code, you can change it to an if expression instead:

let f = open::that("assets/maps.png");
if f.is_err() {
    open::that("assets/map/maps.png").unwrap();
}

For completeness, here's one last way to write this, using Result::or_else:

open::that("assets/maps.png").or_else(|_|
    open::that("assets/map/maps.png")
).unwrap();
4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.