]> git.proxmox.com Git - rustc.git/blob - src/doc/reference/src/expressions/grouped-expr.md
New upstream version 1.52.0~beta.3+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 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.
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
19 An example of a necessary use of parentheses is when calling a function pointer that is a member of a struct:
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"};
31 #
32 assert_eq!( a.f (), "The method f");
33 assert_eq!((a.f)(), "The field f");
34 ```
35
36 ## Group expression attributes
37
38 [Inner attributes] are allowed directly after the opening parenthesis of a group expression in the same expression contexts as [attributes on block expressions].
39
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