Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I ignore an error returned from a Rust function and proceed regardless?

Tags:

When it is known that some piece of code might throw an error, we make use of try/catch blocks to ignore such errors and proceed. This is done when the error is not that important but maybe we only want to log it:

try{     int i = 1/0; } catch( ArithmeticException e){     System.out.println("Encountered an error but would proceed."); }  x = y; 

Such a construct in Java would continue on to execute x = y;.

Can I make use of match to do this or any other construct?

I do see a try! macro, but perhaps it would return in case of an error with the return type of the method as Result.

I want to use such a construct in a UT to ensure it continues to run even after an error has occurred.

like image 250
Rajeev Ranjan Avatar asked Jul 02 '18 18:07

Rajeev Ranjan


People also ask

How do I ignore an error?

1. In the Ribbon, (1) go to File > Options. 2. In the Excel Options window, (1) go to the Formulas tab, (2) uncheck Enable background error checking, and (3) click OK.

How do you unwrap error in Rust?

To “unwrap” something in Rust is to say, “Give me the result of the computation, and if there was an error, panic and stop the program.” It would be better if we showed the code for unwrapping because it is so simple, but to do that, we will first need to explore the Option and Result types.

How do you throw an exception in Rust?

Rust doesn't have exceptions. Instead, it has the type Result<T, E> for recoverable errors and the panic! macro that stops execution when the program encounters an unrecoverable error.

How do you return an error in rust?

In Rust, you return something called a Result. The Result<T, E> type is an enum that has two variants - Ok (T) for successful value or Err (E) for error value: Returning errors instead of throwing them is a paradigm shift in error handling.

What is the use of ERR in rust?

It is an enum with the variants, Ok (T), representing success and containing a value, and Err (E), representing error and containing an error value. I highly recommend reading the Error Handling section in the Rust Book: The function will be called, but the result will be ignored. If you omit let _ = , you will get a warning.

How do you handle exceptions in rust?

Unlike other programming languages, Rust does not have exceptions. It returns an enum Result<T, E> for recoverable errors, while it calls the panic macro if the program encounters an unrecoverable error. The panic macro causes the program to exit abruptly.

How does rust handle failure and success?

Rust leverages the type system to communicate that an operation may not succeed: the return type of execute is Result, an enum. The caller is then forced by the compiler to express how they plan to handle both scenarios - success and failure.


1 Answers

Functions in Rust which can fail return a Result:

Result<T, E> is the type used for returning and propagating errors. It is an enum with the variants, Ok(T), representing success and containing a value, and Err(E), representing error and containing an error value.

I highly recommend reading the Error Handling section in the Rust Book:

Rust has a number of features for handling situations in which something goes wrong

If you want to ignore an error, you have different possibilities:

  • Don't use the Result:

      let _ = failing_function(); 

    The function will be called, but the result will be ignored. If you omit let _ = , you will get a warning.

  • Ignore the Err variant of Result using if let or match:

      if let Ok(ret) = failing_function() {       // use the returned value   } 

    You may also convert the Result into Option with Result::ok:

      let opt = failing_function().ok(); 
  • Unwrap the error. This code panics if an error occurred though:

      let ret = failing_function().unwrap();   // or   let ret = failing_function().expect("A panic message to be displayed"); 

try!() unwraps a result and early returns the function, if an error occurred. However, you should use ? instead of try! as this is deprecated.


See also:

  • What is this question mark operator about?
  • Is the question mark operator ? equivalent to the try! macro?
  • How to do error handling in Rust and what are the common pitfalls?
like image 153
Tim Diekmann Avatar answered Oct 23 '22 02:10

Tim Diekmann