]> git.proxmox.com Git - rustc.git/blame - 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
CommitLineData
0531ce1d
XL
1# Grouped expressions
2
8faf50e0
XL
3> **<sup>Syntax</sup>**\
4> _GroupedExpression_ :\
13cf67c4 5> &nbsp;&nbsp; `(` [_InnerAttribute_]<sup>\*</sup> [_Expression_] `)`
0531ce1d 6
6a06907d
XL
7An expression enclosed in parentheses evaluates to the result of the enclosed expression.
8Parentheses can be used to explicitly specify evaluation order within an expression.
0531ce1d
XL
9
10An example of a parenthesized expression:
11
12```rust
13let x: i32 = 2 + 3 * 4;
14let y: i32 = (2 + 3) * 4;
15assert_eq!(x, 14);
16assert_eq!(y, 20);
17```
18
6a06907d 19An 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
32assert_eq!( a.f (), "The method f");
33assert_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