about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-04-17 06:25:15 +0200
committerGitHub <noreply@github.com>2025-04-17 06:25:15 +0200
commit6426050b68a4455db62131fd463b28fa063073ce (patch)
tree33f09c758a11c63f692dcb39d917a9fa1429ce32 /src
parent0de803c38d33655ed3ec25e237b6c1e2bb4e7426 (diff)
parent4ac3877521084501c44d3bea1251398448cb7ecd (diff)
downloadrust-6426050b68a4455db62131fd463b28fa063073ce.tar.gz
rust-6426050b68a4455db62131fd463b28fa063073ce.zip
Rollup merge of #139416 - mejrs:metavar, r=tgross35
unstable book; document `macro_metavar_expr_concat`

Rendered:

![image](https://github.com/user-attachments/assets/25c6d1fa-dac4-4c28-869f-e053b9384e59)
![image](https://github.com/user-attachments/assets/caa11e61-6321-4646-bf53-a1d31fca39f4)
![image](https://github.com/user-attachments/assets/52dc788f-1764-4a75-9fc3-e0fdefd30642)
![image](https://github.com/user-attachments/assets/2fd2ba06-b8e3-4341-83ec-0ea686efdde2)

cc `@c410-f3r`
Diffstat (limited to 'src')
-rw-r--r--src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md133
-rw-r--r--src/doc/unstable-book/src/language-features/macro-metavar-expr.md10
-rw-r--r--src/doc/unstable-book/src/library-features/concat-idents.md2
3 files changed, 145 insertions, 0 deletions
diff --git a/src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md b/src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
new file mode 100644
index 00000000000..b6dbdb14407
--- /dev/null
+++ b/src/doc/unstable-book/src/language-features/macro-metavar-expr-concat.md
@@ -0,0 +1,133 @@
+# `macro_metavar_expr_concat`
+
+The tracking issue for this feature is: [#124225]
+
+------------------------
+
+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`].
+ `#![feature(macro_metavar_expr_concat)]` introduces a way to do this, using the concat metavariable expression.
+
+> This feature uses the syntax from [`macro_metavar_expr`] but is otherwise
+> independent. It replaces the old unstable feature [`concat_idents`].
+
+> This is an experimental feature; it and its syntax will require a RFC before stabilization.
+
+
+### Overview
+
+`#![feature(macro_metavar_expr_concat)]` provides the `concat` metavariable expression for creating new identifiers:
+
+```rust
+#![feature(macro_metavar_expr_concat)]
+
+macro_rules! create_some_structs {
+    ($name:ident) => {
+        pub struct ${ concat(First, $name) };
+        pub struct ${ concat(Second, $name) };
+        pub struct ${ concat(Third, $name) };
+    }
+}
+
+create_some_structs!(Thing);
+```
+
+This macro invocation expands to:
+
+```rust
+pub struct FirstThing;
+pub struct SecondThing;
+pub struct ThirdThing;
+```
+
+### Syntax
+
+This feature builds upon the metavariable expression syntax `${ .. }` as specified in [RFC 3086] ([`macro_metavar_expr`]).
+ `concat` is available like `${ concat(items) }`, where `items` is a comma separated sequence of idents and/or literals.
+
+### Examples
+
+#### Create a function or method with a concatenated name
+
+```rust
+#![feature(macro_metavar_expr_concat)]
+
+macro_rules! make_getter {
+    ($name:ident, $field: ident, $ret:ty) => {
+        impl $name {
+            pub fn ${ concat(get_, $field) }(&self) -> &$ret {
+                &self.$field
+            }
+        }
+    }
+}
+
+pub struct Thing {
+    description: String,
+}
+
+make_getter!(Thing, description, String);
+```
+
+This expands to:
+
+```rust
+pub struct Thing {
+    description: String,
+}
+
+impl Thing {
+    pub fn get_description(&self) -> &String {
+        &self.description
+    }
+}
+```
+
+#### Create names for macro generated tests
+
+```rust
+#![feature(macro_metavar_expr_concat)]
+
+macro_rules! test_math {
+    ($integer:ident) => {
+        #[test]
+        fn ${ concat(test_, $integer, _, addition) } () {
+            let a: $integer = 73;
+            let b: $integer = 42;
+            assert_eq!(a + b, 115)
+        }
+
+        #[test]
+        fn ${ concat(test_, $integer, _, subtraction) } () {
+            let a: $integer = 73;
+            let b: $integer = 42;
+            assert_eq!(a - b, 31)
+        }
+    }
+}
+
+test_math!(i32);
+test_math!(u64);
+test_math!(u128);
+```
+
+Running this returns the following output:
+
+```text
+running 6 tests
+test test_i32_subtraction ... ok
+test test_i32_addition ... ok
+test test_u128_addition ... ok
+test test_u128_subtraction ... ok
+test test_u64_addition ... ok
+test test_u64_subtraction ... ok
+
+test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
+```
+
+[`paste`]: https://crates.io/crates/paste
+[RFC 3086]: https://rust-lang.github.io/rfcs/3086-macro-metavar-expr.html
+[`concat_idents!`]: https://doc.rust-lang.org/nightly/std/macro.concat_idents.html
+[`macro_metavar_expr`]: ../language-features/macro-metavar-expr.md
+[`concat_idents`]: ../library-features/concat-idents.md
+[#124225]: https://github.com/rust-lang/rust/issues/124225
+[declarative macros]: https://doc.rust-lang.org/stable/reference/macros-by-example.html
diff --git a/src/doc/unstable-book/src/language-features/macro-metavar-expr.md b/src/doc/unstable-book/src/language-features/macro-metavar-expr.md
new file mode 100644
index 00000000000..7ce64c1a354
--- /dev/null
+++ b/src/doc/unstable-book/src/language-features/macro-metavar-expr.md
@@ -0,0 +1,10 @@
+# `macro_metavar_expr`
+
+The tracking issue for this feature is: [#83527]
+
+------------------------
+
+> This feature is not to be confused with [`macro_metavar_expr_concat`].
+
+[`macro_metavar_expr_concat`]: ./macro-metavar-expr-concat.md
+[#83527]: https://github.com/rust-lang/rust/issues/83527
diff --git a/src/doc/unstable-book/src/library-features/concat-idents.md b/src/doc/unstable-book/src/library-features/concat-idents.md
index 73f6cfa2178..4366172fb99 100644
--- a/src/doc/unstable-book/src/library-features/concat-idents.md
+++ b/src/doc/unstable-book/src/library-features/concat-idents.md
@@ -6,6 +6,8 @@ The tracking issue for this feature is: [#29599]
 
 ------------------------
 
+> This feature is expected to be superseded by [`macro_metavar_expr_concat`](../language-features/macro-metavar-expr-concat.md).
+
 The `concat_idents` feature adds a macro for concatenating multiple identifiers
 into one identifier.