4 releases (1 stable)

1.0.0 Apr 6, 2020
1.0.0-alpha.2 Nov 21, 2019
1.0.0-alpha.1 Sep 23, 2019
1.0.0-alpha.0 Sep 20, 2019

#593 in Rust patterns

Download history 1157/week @ 2021-09-26 935/week @ 2021-10-03 1033/week @ 2021-10-10 1047/week @ 2021-10-17 1050/week @ 2021-10-24 1372/week @ 2021-10-31 1019/week @ 2021-11-07 1501/week @ 2021-11-14 1273/week @ 2021-11-21 1158/week @ 2021-11-28 866/week @ 2021-12-05 846/week @ 2021-12-12 892/week @ 2021-12-19 612/week @ 2021-12-26 886/week @ 2022-01-02 749/week @ 2022-01-09

3,203 downloads per month
Used in 86 crates (61 directly)


92 lines

Der Fehler


Der Fehler is a library to add support for "throwing functions" to Rust through procedural macros. Functions marked with the throws attribute return Result, but the "Ok" path is used by default and you don't need to wrap ok return values in Ok. To throw errors, use ? or the throws macro.

Der Fehler provides these items:

The #[throws] attribute

The throws attribute modifies a function or method to make it return a Result. It takes an optional typename as an argument to the attribute which will be the error type of this function; if no typename is supplied, it uses the default error type for this crate.

Within the function body, returns (including the implicit final return) are automatically "Ok-wrapped." To raise errors, use ? or the throws! macro.

For example, these two functions are equivalent:

fn foo(x: bool) -> i32 {
    if x {
    } else {

fn bar(x: bool) -> Result<i32, i32> {
    if x {
    } else {

In functions that return Option

The attribute can be used to make a function that returns an Option using the as Option syntax, demonstrated below:

// This function returns `Option<i32>`
#[throws(as Option)]
fn foo(x: bool) -> i32 {
    if x {
        return 0;
    } else {

The throw! macro

throw! is a macro which is equivalent to the Err($e)? pattern. It takes an error type and "throws" it.

One important aspect of the throw! macro is that it allows you to return errors inside of functions marked with throws. You cannot just return errors from these functions, you need to use this macro.


  • Make throws work on closures and async blocks (attributes are not allowed on expressions on stable)
  • Make throws work on Try types other than Result and Option (TRy is not on stable).


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~17K SLoC