Return result of Command::execute directly from main #7

Closed
MarkusPettersson98 wants to merge 1 commit from main-returns-result into main
MarkusPettersson98 commented 2023-10-29 16:07:37 +01:00 (Migrated from github.com)

Since Result implements Termination, the result of Command::execute can be returned from main directly. If an FF2MpvError error would be propagated to main, it will be printed using eprintln just as before, and the program will terminate with an appropriate exit code: https://doc.rust-lang.org/std/process/struct.ExitCode.html#associatedconstant.FAILURE

The only caveat is that FF2MpvError has to implement Debug, but instead of deriving it I cheated a bit and re-used the existing Display implementation to preserve the format of the error messages.

Thanks for this helpful little program 💚

Since `Result` [implements `Termination`](https://doc.rust-lang.org/std/process/trait.Termination.html), the result of `Command::execute` can be returned from `main` directly. If an `FF2MpvError` error would be propagated to `main`, it will be printed using `eprintln` just as before, and the program will terminate with an appropriate exit code: https://doc.rust-lang.org/std/process/struct.ExitCode.html#associatedconstant.FAILURE The only caveat is that `FF2MpvError` has to implement `Debug`, but instead of deriving it I cheated a bit and re-used the existing `Display` implementation to preserve the format of the error messages. Thanks for this helpful little program :green_heart:
ryze312 commented 2023-10-30 22:32:31 +01:00 (Migrated from github.com)

I don't think this is meant to be used in production code (note it uses Debug trait instead of Display). This requires Rust's "runtime" to wrap the main function and handle error outside of the developer's control. Returning Result from main should be used for faster prototyping and never appear in release, at least in my opinion.

I don't think this is meant to be used in production code (note it uses `Debug` trait instead of `Display`). This requires Rust's "runtime" to wrap the `main` function and handle error outside of the developer's control. Returning `Result` from `main` should be used for faster prototyping and never appear in release, at least in my opinion.
ryze312 commented 2023-11-28 19:36:54 +01:00 (Migrated from github.com)

No follow up in a month, closing

No follow up in a month, closing

Pull request closed

Sign in to join this conversation.
No description provided.