Rust-reference/expressions/grouped-expr

来自菜鸟教程
跳转至:导航、​搜索

Grouped expressions

分组表达式

grouped-expr.md

commit: b0e0ad6490d6517c19546b1023948986578fc378
本章译文最后维护日期:2020-11-12

句法

GroupedExpression :
   ( InnerAttribute* Expression )

由圆括号封闭的表达式的求值结果就是在其内的表达式的求值结果。在表达式内部,圆括号可用于显式地指定表达式内部的求值顺序。

圆括号表达式的一个例子:

let x: i32 = 2 + 3 * 4;
let y: i32 = (2 + 3) * 4;
assert_eq!(x, 14);
assert_eq!(y, 20);

当调用结构体的函数指针类型的成员时,必须使用括号,示例如下:

# struct A {
#    f: fn() -> &'static str
# }
# impl A {
#    fn f(&self) -> &'static str {
#        "The method f"
#    }
# }
# let a = A{f: || "The field f"};
#
assert_eq!( a.f (), "The method f");
assert_eq!((a.f)(), "The field f");

Group expression attributes

分组表达式上的属性

在允许块表达式上的属性存在的那几种表达式上下文中,可以在分组表达式的左括号后直接使用内部属性