A new exception handling API in the recently announced Rust 1.9 adds more control when dealing with cross-thread exception propagation. The new version also brings better compiler performance when deciding variable equality.
As mentioned, a new catch_unwind
API available in std::panic
allows developers to better control the stack unwinding process triggered by an exception, which is used in Rust to handle unexpected problems. Rust philosophy in such cases is “failing fast”, which means bringing the raising thread to a halt after unwinding its stack. Other threads however, are left untouched and are allowed to continue running until they try to communicate with the panicked thread, in which case they will need to do some kind of recovery. By using catch_unwind
developers will be able to catch a panic
and convert it into a common error within the failing thread:
let result = panic::catch_unwind(|| {
panic!("oh no!");
});
assert!(result.is_err());
This should be most useful in two scenarios:
- When embedding Rust in other languages. In this case unwinding is likely to produce a segmentation fault at the language boundary.
- When, for example, creating libraries to manage threads. In this case, instead of halting the failing thread, it could be preferable to communicate the error to the client.
It is worth noting that stack unwinding is currently the only strategy for handling panic
in Rust, but this is going to change in the future. Indeed, a new “abort” strategy will allow to avoid paying the cost of stack unwinding when that may be preferable.
The new Rust version also brings better compiler performance when deciding whether two variables are equal. Markus Westerlind, submitter of the PR, claims an impressive improvement in certain cases, going from a complexity of O(n!)
to (O(n)
. As Westerlind explained on Reddit, although n
is usually small, subtle differences in code may make n
blow up. In such cases, as demonstrated in Westerlind’s combine
library, Rust 1.9 is going to provide a big performance improvement.
Rust 1.9 also contributes to stabilizing more library functions, including networking, encoding, and pointer handling functions.
You can find more details about Rust 1.9 in the official announcement.