try!
and type Result
, until an indefinite moment after the first release of the Rust programming language.Result { Ok(value), Err(why) }
and try!
in Rust try!
. The type Result
is an enumeration (enum), similar to Option { Some(value), None }
and associated with it in meaning. Option type None
indicates a missing value, while Result
type Err(why)
specifies why the value is missing.Result
type from functions to pass the return value or the reason for which the value could not be returned. Macro try!
in turn, allows you to automatically return Err(why)
from the current function if the call to another function failed (applied to an object of type Result
). enum MathError { DivisionByZero } // , fn div(x: f64, y: f64) -> Result<f64, MathError> { if y == 0.0 { Err(DivisionByZero) // } else { Ok(x / y) } } // Result fn div_user() -> () { match div(1.0, 0.0) { Err(why) => /* , why MathError */ Ok(result) => /* result f64 */ } } // fn error_proxy() -> Result<f64, MathError> { let result1 = try!(div(1.0, 0.0)); let result2 = try!(div(1.0, result1)); div(1, result2); }
Source: https://habr.com/ru/post/242269/
All Articles