Skip to content

Commit fc95547

Browse files
committed
Moved grouped expressions to its own page. Added another example of its use.
1 parent e490727 commit fc95547

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [Path expressions](expressions/path-expr.md)
4646
- [Block expressions](expressions/block-expr.md)
4747
- [Operator expressions](expressions/operator-expr.md)
48+
- [Grouped expressions](expressions/grouped-expr.md)
4849
- [Array and index expressions](expressions/array-expr.md)
4950
- [Tuple and index expressions](expressions/tuple-expr.md)
5051
- [Struct expressions](expressions/struct-expr.md)

src/expressions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ Many of the following operators and expressions can also be overloaded for
247247
other types using traits in `std::ops` or `std::cmp`. These traits also
248248
exist in `core::ops` and `core::cmp` with the same names.
249249

250+
[grouped]: expressions/grouped-expr.html
250251
[block expressions]: expressions/block-expr.html
251252
[call expressions]: expressions/call-expr.html
252253
[closure expressions]: expressions/closure-expr.html
@@ -272,7 +273,6 @@ exist in `core::ops` and `core::cmp` with the same names.
272273
[dereferences]: expressions/operator-expr.html#the-dereference-operator
273274
[dereferencing]: expressions/operator-expr.html#the-dereference-operator
274275
[dereference operator]: expressions/operator-expr.html#the-dereference-operator
275-
[grouped]: expressions/operator-expr.html#grouped-expressions
276276
[lazy boolean]: expressions/operator-expr.html#lazy-boolean-operators
277277
[negation]: expressions/operator-expr.html#negation-operators
278278
[overflow]: expressions/operator-expr.html#overflow

src/expressions/grouped-expr.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Grouped expressions
2+
3+
> **<sup>Syntax</sup>**
4+
> _GroupedExpression_ :
5+
> &nbsp;&nbsp; `(` [_Expression_] `)`
6+
7+
An expression enclosed in parentheses evaluates to the result of the enclosed
8+
expression. Parentheses can be used to explicitly specify evaluation order
9+
within an expression.
10+
11+
This operator cannot be overloaded.
12+
13+
An example of a parenthesized expression:
14+
15+
```rust
16+
let x: i32 = 2 + 3 * 4;
17+
let y: i32 = (2 + 3) * 4;
18+
assert_eq!(x, 14);
19+
assert_eq!(y, 20);
20+
```
21+
22+
An example of a necessary use of parentheses is when calling a function pointer
23+
that is a member of a struct:
24+
25+
```rust
26+
# struct A {
27+
# f: fn() -> &'static str
28+
# }
29+
# impl A {
30+
# fn f(&self) -> &'static str {
31+
# "The method f"
32+
# }
33+
# }
34+
# let a = A{f: || "The field f"};
35+
#
36+
assert_eq!( a.f (), "The method f");
37+
assert_eq!( (a.f)(), "The field f");
38+
```
39+
40+
[_Expression_]: expressions.html

src/expressions/operator-expr.md

-21
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,6 @@ overflow:
3232
* Using `<<` or `>>` where the right-hand argument is greater than or equal to
3333
the number of bits in the type of the left-hand argument, or is negative.
3434

35-
## Grouped expressions
36-
37-
> **<sup>Syntax</sup>**
38-
> _GroupedExpression_ :
39-
> &nbsp;&nbsp; `(` [_Expression_] `)`
40-
41-
An expression enclosed in parentheses evaluates to the result of the enclosed
42-
expression. Parentheses can be used to explicitly specify evaluation order
43-
within an expression.
44-
45-
This operator cannot be overloaded.
46-
47-
An example of a parenthesized expression:
48-
49-
```rust
50-
let x: i32 = 2 + 3 * 4;
51-
let y: i32 = (2 + 3) * 4;
52-
assert_eq!(x, 14);
53-
assert_eq!(y, 20);
54-
```
55-
5635
## Borrow operators
5736

5837
> **<sup>Syntax</sup>**

0 commit comments

Comments
 (0)