Rust errors: Difference between revisions

From wikinotes
(Created page with "Rust has two primary methods of handling errors. * <code>panic!()</code> halts/exits the program * <code>Result</code> types are for handle-able errors = panic = <blockquote> <syntaxhighlight lang="rust"> </syntaxhighlight> </blockquote><!-- panic --> = Result = <blockquote> <syntaxhighlight lang="rust"> </syntaxhighlight> </blockquote><!-- Result -->")
 
Line 5: Line 5:
= panic =
= panic =
<blockquote>
<blockquote>
* intended for halting application, not control flow
* have backtraces
<syntaxhighlight lang="rust">
<syntaxhighlight lang="rust">
panic!("tried to X but couldn't Y")


// convert panic to result
// (not intended for native rust code)
let result = panic::catch_unwind(|| {
    panic!("oh no!");
});
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- panic -->
</blockquote><!-- panic -->

Revision as of 17:18, 8 February 2023

Rust has two primary methods of handling errors.

  • panic!() halts/exits the program
  • Result types are for handle-able errors

panic

  • intended for halting application, not control flow
  • have backtraces
panic!("tried to X but couldn't Y")

// convert panic to result
// (not intended for native rust code)
let result = panic::catch_unwind(|| {
    panic!("oh no!");
});

Result