分组表达式

语法
GroupedExpression :
   ( 表达式 )

一个 分组表达式 包装单个表达式,并计算为该表达式的值。分组表达式的语法是一个 (,然后是一个表达式(称为 被括起来的操作数),然后是一个 )

分组表达式计算为被括起来的操作数的值。

与其他表达式不同,分组表达式既是 位置表达式也是值表达式。当被括起来的操作数是位置表达式时,它就是位置表达式;当被括起来的操作数是值表达式时,它就是值表达式。

括号可用于明确修改表达式中子表达式的优先级顺序。

分组表达式的一个示例

#![allow(unused)]
fn main() {
let x: i32 = 2 + 3 * 4; // not parenthesized
let y: i32 = (2 + 3) * 4; // parenthesized
assert_eq!(x, 14);
assert_eq!(y, 20);
}

一个必须使用括号的例子是调用作为结构体成员的函数指针时

#![allow(unused)]
fn main() {
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");
}