📜 ⬆️ ⬇️

There will be no exceptions in Rust 1.0

Rust Logo Today, Aaron Tyuron - a developer who recently joined the development of Rust in Mozilla - has announced the postponement of the implementation of any exception mechanism besides the already existing macro try! and type Result , until an indefinite moment after the first release of the Rust programming language.

This means that in Rust 1.0 there will be no first-class exceptions — that is, fully integrated with other features of the language.

To handle errors at the moment, there is a 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.
')
Rust offers to return the 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 ).

This is what the code looks like:
 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); } 

This is where the Rust capabilities in terms of error handling, in essence, end.

Rust is a low-level programming language with a powerful static analysis system. For example, static analysis in Rust does not allow careless work with memory. You can learn more about Rust on the official website , and you can look at code examples with parsing, for example, on rustbyexample.com : Option , Result , try! . The language is being developed by the non-profit organization Mozilla, and the release of Rust 1.0 is scheduled for early 2015.

Source: https://habr.com/ru/post/242269/


All Articles