]> git.proxmox.com Git - rustc.git/blob - src/doc/reference/src/expressions/grouped-expr.md
New upstream version 1.53.0+dfsg1
[rustc.git] / src / doc / reference / src / expressions / grouped-expr.md
1 # Grouped expressions
2
3 > **<sup>Syntax</sup>**\
4 > _GroupedExpression_ :\
5 > &nbsp;&nbsp; `(` [_InnerAttribute_]<sup>\*</sup> [_Expression_] `)`
6
7 A *parenthesized expression* wraps a single expression, evaluating to that expression.
8 The syntax for a parenthesized expression is a `(`, then an expression, called the *enclosed operand*, and then a `)`.
9
10 Parenthesized expressions evaluate to the value of the enclosed operand.
11 Unlike other expressions, parenthesized expressions are both [place expressions and value expressions][place].
12 When the enclosed operand is a place expression, it is a place expression and when the enclosed operand is a value expression, it is a value expression.
13
14 Parentheses can be used to explicitly modify the precedence order of subexpressions within an expression.
15
16 An example of a parenthesized expression:
17
18 ```rust
19 let x: i32 = 2 + 3 * 4;
20 let y: i32 = (2 + 3) * 4;
21 assert_eq!(x, 14);
22 assert_eq!(y, 20);
23 ```
24
25 An example of a necessary use of parentheses is when calling a function pointer that is a member of a struct:
26
27 ```rust
28 # struct A {
29 # f: fn() -> &'static str
30 # }
31 # impl A {
32 # fn f(&self) -> &'static str {
33 # "The method f"
34 # }
35 # }
36 # let a = A{f: || "The field f"};
37 #
38 assert_eq!( a.f (), "The method f");
39 assert_eq!((a.f)(), "The field f");
40 ```
41
42 ## Group expression attributes
43
44 [Inner attributes] are allowed directly after the opening parenthesis of a group expression in the same expression contexts as [attributes on block expressions].
45
46 [Inner attributes]: ../attributes.md
47 [_Expression_]: ../expressions.md
48 [_InnerAttribute_]: ../attributes.md
49 [attributes on block expressions]: block-expr.md#attributes-on-block-expressions
50 [place]: ../expressions.md#place-expressions-and-value-expressions