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: src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
+30-32
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@ The tracking issue for this feature is: [#124225]
4
4
5
5
------------------------
6
6
7
-
8
-
`#![feature(macro_metavar_expr_concat)]`provides a more powerful alternative to [`concat_idents!`].
7
+
In stable Rust, there is no way to create new identifiers by joining identifiers to literals or other identifiers without using procedural macros such as [`paste`].
8
+
`#![feature(macro_metavar_expr_concat)]`introduces a way to do this, using the concat metavariable expression.
9
9
10
10
> This feature is not to be confused with [`macro_metavar_expr`] or [`concat_idents`].
11
11
@@ -14,36 +14,35 @@ The tracking issue for this feature is: [#124225]
14
14
15
15
### Overview
16
16
17
-
`macro_rules!`macros cannot create new identifiers and use them in ident positions.
18
-
A common use case is the need to create new structs or functions. The following cannot be done[^1]:
17
+
At this time, [declarative macros] cannot create new identifiers.
18
+
A common use case is the need to create names for structs or functions. The following cannot be done on stable Rust[^1]:
19
19
20
20
```rust,compile_fail
21
21
macro_rules! create_some_structs {
22
-
($name:ident) => {
23
-
// Invalid syntax
24
-
struct First_$name;
25
-
// Also invalid syntax
26
-
struct Second_($name);
27
-
// Macros are not allowed in this position
28
-
// (This restriction is what makes `concat_idents!` useless)
29
-
struct concat_idents!(Third_, $name);
30
-
}
22
+
($name:ident) => {
23
+
// Invalid syntax
24
+
pub struct First$name;
25
+
// Also invalid syntax
26
+
pub struct Second($name);
27
+
// Macros are not allowed in this position
28
+
// (This restriction is what makes `concat_idents!` useless)
29
+
pub struct concat_idents!(Third, $name);
30
+
}
31
31
}
32
32
# create_some_structs!(Thing);
33
33
```
34
34
35
-
`#![feature(macro_metavar_expr_concat)]` provides the `concat` metavariable to concatenate idents in ident position:
35
+
`#![feature(macro_metavar_expr_concat)]` provides the `concat` metavariable to concatenate idents:
36
36
37
37
```rust
38
38
#![feature(macro_metavar_expr_concat)]
39
-
# #![allow(non_camel_case_types, dead_code)]
40
39
41
40
macro_rules!create_some_structs {
42
-
($name:ident) => {
43
-
struct${ concat(First_, $name) };
44
-
struct${ concat(Second_, $name) };
45
-
struct${ concat(Third_, $name) };
46
-
}
41
+
($name:ident) => {
42
+
pubstruct${ concat(First, $name) };
43
+
pubstruct${ concat(Second, $name) };
44
+
pubstruct${ concat(Third, $name) };
45
+
}
47
46
}
48
47
49
48
create_some_structs!(Thing);
@@ -52,10 +51,9 @@ create_some_structs!(Thing);
52
51
This macro invocation expands to:
53
52
54
53
```rust
55
-
# #![allow(non_camel_case_types, dead_code)]
56
-
structFirst_Thing;
57
-
structSecond_Thing;
58
-
structThird_Thing;
54
+
pubstructFirstThing;
55
+
pubstructSecondThing;
56
+
pubstructThirdThing;
59
57
```
60
58
61
59
### Syntax
@@ -69,16 +67,15 @@ This feature builds upon the metavariable expression syntax `${ .. }` as specifi
69
67
70
68
```rust
71
69
#![feature(macro_metavar_expr_concat)]
72
-
# #![allow(non_camel_case_types, dead_code)]
73
70
74
71
macro_rules!make_getter {
75
-
($name:ident, $field:ident, $ret:ty) => {
76
-
impl$name {
77
-
pubfn${ concat(get_, $field) }(&self) ->&$ret {
78
-
&self.$field
79
-
}
80
-
}
81
-
}
72
+
($name:ident, $field:ident, $ret:ty) => {
73
+
impl$name {
74
+
pubfn${ concat(get_, $field) }(&self) ->&$ret {
75
+
&self.$field
76
+
}
77
+
}
78
+
}
82
79
}
83
80
84
81
pubstructThing {
@@ -152,3 +149,4 @@ test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; fini
0 commit comments