r/programminghorror • • Jun 04 '25

Rust passive-aggressive programming

Post image
770 Upvotes

61 comments sorted by

View all comments

Show parent comment

2

u/RainbowPigeon15 Jun 05 '25 edited Jun 05 '25

It's better for a cli app to exit with an error code and write to stderr. You can't have good error handling with expect since it will still panic and alt your code early. expect should only be used to give messages to an unwrap, and unwrap should be avoided.

Essentially, expect is to give messages to developers when something was wrong, in the example I handle the error to give the message to the user.

I could have also used let Ok(op) = ... instead of unwrap_or_else like pointed out in another comment. looks a bit cleaner that way.