You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guide/expressions.md
+42-2Lines changed: 42 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -634,7 +634,7 @@ never use a block (unless the block is empty).
634
634
635
635
If the right-hand side consists of multiple statements or has line comments or
636
636
the start of the line cannot be fit on the same line as the left-hand side, use
637
-
a block.
637
+
a block. A block may also be used in cases where the right-hand side is a macro call expression to prevent issues with expansions containing a trailing semicolon, more details [below](#macro-call-expressions).
638
638
639
639
The body of a block arm should be block indented once.
640
640
@@ -661,7 +661,7 @@ match foo {
661
661
```
662
662
663
663
If the body is a single expression with no line comments and not a control flow
664
-
expression, then it may be started on the same line as the right-hand side. If
664
+
expression, then it may be started on the same line as the left-hand side. If
665
665
not, then it must be in a block. Example,
666
666
667
667
```rust
@@ -777,6 +777,46 @@ We define a pattern clause to be *small* if it matches the following grammar:
777
777
778
778
E.g., `&&Some(foo)` matches, `Foo(4, Bar)` does not.
779
779
780
+
#### Macro call expressions
781
+
When the right-hand side of a match arm contains a macro call expression, it may be necessary to use a block to prevent issues in expansion.
782
+
783
+
In some cases the right-hand side may be placed on the same line as the left-hand side. E.g.,
784
+
785
+
```rust
786
+
macro_rules!expr {
787
+
() => {
788
+
true
789
+
};
790
+
}
791
+
792
+
fnmain() {
793
+
let_val:bool=matchtrue {
794
+
true=>expr!(),
795
+
false=>false,
796
+
};
797
+
}
798
+
```
799
+
800
+
However, in other cases it is necessary to use a block to prevent issues in macro expansion, such as with trailing semicolons.
801
+
802
+
```rust
803
+
macro_rules!stmt {
804
+
() => {
805
+
true;
806
+
};
807
+
}
808
+
809
+
fnmain() {
810
+
matchtrue {
811
+
true=> {
812
+
stmt!()
813
+
}
814
+
false=> {}
815
+
}
816
+
}
817
+
```
818
+
819
+
Note that at the time of this writing [rustc ignores these trailing semicolons](https://github.com/rust-lang/rust/issues/33953), but this guidance is provided in case that changes.
0 commit comments