Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the question mark operator ? equivalent to the try! macro?

Tags:

operators

rust

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?

like image 462
arkod Avatar asked Nov 11 '16 09:11

arkod


People also ask

What does the question mark do in Rust?

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.

How do you use the question mark operator in Rust?

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 .

What does do in Rust?

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.


1 Answers

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).

like image 87
Chris Emerson Avatar answered Sep 22 '22 21:09

Chris Emerson