2

When a function errors in a test, I use this pattern:

if let Err(err) = func() {
    panic!("got error on export: {:?}", err);
}

Is there a macro for asserting an Ok result and get the Err printed otherwise?

hpk42
  • 21,501
  • 4
  • 47
  • 53
  • 1
    `func().expect("got error on export")`. – Shepmaster Oct 26 '20 at 14:34
  • 1
    [Shortcuts for Panic on Error: `unwrap` and `expect`](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#shortcuts-for-panic-on-error-unwrap-and-expect) – Shepmaster Oct 26 '20 at 14:36
  • If you need a dynamic panic message, `unwrap_or_else` is quite useful, e.g.: `let file = File::open(file_name).unwrap_or_else(|e| panic!("opening {}: {}", file_name, e));` – user4815162342 Oct 26 '20 at 16:53

0 Answers0