Terminator
A small library to have Display
output for fn main() -> Result<(), E>
The Problem
In [RFC 1937][RFC] Rust added a long wanted feature: using ?
in fn main()
.
In the RFC it had a trait bound for E
to be Display
, but in the actual
version that was stabilized it had E
be Debug
. While fine if one wants to
have Debug
output in say tests, it has no use for those who want to use it
in a binary program that people would want to use. To get around this people
continue to use the same pattern we had before rustc 1.26.0
, namely:
What we want is this code, but it outputs a Display
value:
That's where the Terminator
library comes in. The code for this library is
all in one file so it's fairly easy to read, take a look at it for an
explanation of how it works exactly. As long as your error implements
std::error::Error
then this should work!
How to use it
Just have your main
function return Return<(), Terminator>
or if you need
to use it's never type implementation Return<!, Terminator>
. Your code should
look something like this:
use Terminator;
Minimum version
We support a minimum rustc
version of 1.26.0
as this was when the question
mark in main feature was stabilized. However, for versions less than 1.31.0
you'll need to set the feature flag rust2015
in your Cargo.toml
like so:
[]
= { = "0.1", = false, = "rust2015" }
This is to get around that dyn
is required in 2018, while in 2015 it was not.
License
See the LICENSE file for a copy of the MPL 2.0
distributed with
this source code. By contributing to this code repository you agree to have your
code also distributed and released under these terms.
Support
This is a small library but if it put a smile on your face or made your day just a little bit better then consider buying me a cup of coffee or getting my dog a chew toy.