For example, I want to remove a UNIX socket file, ignoring "file not found" errors (just like what rm -f does). But at the same time, I want all other kinds of errors (e.g. permission denied, file in use) to be reported as is.
What's the idiomatic way to do this? This is what I'm currently doing:
match std::fs::remove_file("/tmp/server.sock") {
Err(err) if err.kind() != ErrorKind::NotFound => {
panic!("failed to remove /tmp/server.sock: {}", err);
}
_ => (),
}
Note that the expect method will add the error description in case of a panic:
thread 'main' panicked at 'failed to remove testfile: Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" }', src/main.rs:10:10
But looking at @2e71828's reply theirs seems much shorter than my approach… So perhaps that's better or at least a good alternative.
So maybe I shouldn't underestimate the power of using manual pattern matching instead of using methods like or_else and the like. The advantage of match is that you can throw an error inside one of the branches (through r => r?).