]>
Commit | Line | Data |
---|---|---|
0531ce1d XL |
1 | # Grouped expressions |
2 | ||
8faf50e0 XL |
3 | > **<sup>Syntax</sup>**\ |
4 | > _GroupedExpression_ :\ | |
13cf67c4 | 5 | > `(` [_InnerAttribute_]<sup>\*</sup> [_Expression_] `)` |
0531ce1d | 6 | |
6a06907d XL |
7 | An expression enclosed in parentheses evaluates to the result of the enclosed expression. |
8 | Parentheses can be used to explicitly specify evaluation order within an expression. | |
0531ce1d XL |
9 | |
10 | An example of a parenthesized expression: | |
11 | ||
12 | ```rust | |
13 | let x: i32 = 2 + 3 * 4; | |
14 | let y: i32 = (2 + 3) * 4; | |
15 | assert_eq!(x, 14); | |
16 | assert_eq!(y, 20); | |
17 | ``` | |
18 | ||
6a06907d | 19 | An example of a necessary use of parentheses is when calling a function pointer that is a member of a struct: |
0531ce1d XL |
20 | |
21 | ```rust | |
22 | # struct A { | |
23 | # f: fn() -> &'static str | |
24 | # } | |
25 | # impl A { | |
26 | # fn f(&self) -> &'static str { | |
27 | # "The method f" | |
28 | # } | |
29 | # } | |
30 | # let a = A{f: || "The field f"}; | |
8faf50e0 | 31 | # |
0531ce1d XL |
32 | assert_eq!( a.f (), "The method f"); |
33 | assert_eq!((a.f)(), "The field f"); | |
34 | ``` | |
35 | ||
13cf67c4 XL |
36 | ## Group expression attributes |
37 | ||
6a06907d | 38 | [Inner attributes] are allowed directly after the opening parenthesis of a group expression in the same expression contexts as [attributes on block expressions]. |
13cf67c4 | 39 | |
416331ca XL |
40 | [Inner attributes]: ../attributes.md |
41 | [_Expression_]: ../expressions.md | |
42 | [_InnerAttribute_]: ../attributes.md | |
43 | [attributes on block expressions]: block-expr.md#attributes-on-block-expressions |