Crate expect_macro [] [src]

The expect! macro

This crate adds the expect! macro, which is basically just calling .expect on your type but also:

  • Includes the exact line number of the error
  • Allows you to specify a custom error message with formatting.
  • Lazy evaluates error conditions (unlike result.expect(&format!(...)))

This gives you panic messages like this:

thread 'example' panicked at '"expect error"', src/lib.rs:5:5

As opposed to:

thread 'example' panicked at 'called `Result::unwrap()` on an `Err` value: "expect error"', libcore/result.rs:945:5

Alternatives

If you need to include the Err in a custom error message then do this instead:

let result = Err("expect error");
result.unwrap_or_else(|err| panic!("Got {} but expected 42", err));

Macros

expect

Unwrap a result or panic! with a message.

Traits

IntoResult

Used to ensure either Option or Result are the Result type.