implement the ? operator

The `?` postfix operator is sugar equivalent to the try! macro, but is more amenable to chaining:
`File::open("foo")?.metadata()?.is_dir()`.

`?` is accepted on any *expression* that can return a `Result`, e.g. `x()?`, `y!()?`, `{z}?`,
`(w)?`, etc. And binds more tightly than unary operators, e.g. `!x?` is parsed as `!(x?)`.

cc #31436
This commit is contained in:
Jorge Aparicio
2016-02-28 17:38:48 -05:00
parent c116ae35cf
commit 210dd611aa
12 changed files with 355 additions and 2 deletions

View File

@@ -2277,6 +2277,10 @@ impl<'a> State<'a> {
try!(self.print_inner_attributes_inline(attrs));
try!(self.print_expr(&e));
try!(self.pclose());
},
ast::ExprKind::Try(ref e) => {
try!(self.print_expr(e));
try!(word(&mut self.s, "?"))
}
}
try!(self.ann.post(self, NodeExpr(expr)));