I am looking for a way to chain the usage of the try!
macro in Rust and found this pull request that implements the ?
operator.
It seems like it has been merged, but I cannot seem to find any documentation on it yet. Is it equivalent to the try!
macro or are there important differences?
operator in Rust is used as an error propagation alternative to functions that return Result or Option types. The ? operator is a shortcut as it reduces the amount of code needed to immediately return Err or None from the types Result<T, Err> or Option in a function.
The question mark operatorWhen applied to values of the Result<T, E> type, it propagates errors. If the value is Err(e) , then it will return Err(From::from(e)) from the enclosing function or closure. If applied to Ok(x) , then it will unwrap the value to evaulate to x .
do allows the type to control the state-management between statements, try explicitly disallows the carrying of state between the original type and the resulting type.
Yes, the ?
operator is equivalent to try!()
. ?
is now in stable Rust 1.13, released on November 10, 2016.
The best source of documentation at the moment seems to be RFC 0243. Note that the catch
described in the RFC is not yet implemented at this writing (issue).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With