29

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
arkod
  • 1,973
  • 1
  • 20
  • 20

1 Answers1

42

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

nbro
  • 15,395
  • 32
  • 113
  • 196
Chris Emerson
  • 13,041
  • 3
  • 44
  • 66
  • I seem to remember there were limitations, compared to the macro, in the conversion of errors; was it finally solved? – Matthieu M. Nov 11 '16 at 10:17
  • 3
    @MatthieuM. according to the RFC, it does the same error conversion: 'Like the current try!() macro, the ? operator will also perform an implicit "upcast" on the exception type.' But it'll be nice, given that only part of the RFC has been implemented, to have definitive documentation on what's actually now in the language. :-) – Chris Emerson Nov 11 '16 at 10:30
  • 2
    `?` uses the carrier trait, and for that reason, its type inference is slightly different than when using `try!()`; the latter is `Result`-specific. – bluss Nov 13 '16 at 01:05
  • In case of errors, the `?` operator tends to have much better error messages. – Kornel Apr 16 '18 at 03:18