如何将两个变量连接起来以创建一个声明性宏中的标识符?

huangapple go评论87阅读模式
英文:

How to concat two variables to create an identifier in a declarative macro?

问题

If you want to concatenate the two variables $name and $test_name to output one string, you can use the concat! macro in Rust. Here's how you can modify the line in your code to achieve this:

Replace this line:

  1. fn $name_\$test_name() {

With this line:

  1. fn concat!($name, "_", $test_name)() {

This change will concatenate the two variables $name and $test_name with an underscore in between to create the function name. For example, if $name is foo and $test_name is bar, it will output foo_bar.

英文:

I want to write a macro to support parameterized tests, and got the following code from AI, but got errors on one line:

  1. #[macro_export]
  2. macro_rules! parameterize {
  3. ($name:ident, $params:pat, {$($test_name:ident : ($($args:expr),*)),*}, $test_body:block) => {
  4. $(
  5. fn $name_$test_name() { //<========= this line
  6. let $params = ($($args),*);
  7. $test_body
  8. }
  9. )*
  10. };
  11. }
  12. parameterize! {
  13. should_be_added,
  14. (expected, a, b),
  15. {
  16. positive: (3, 1, 2),
  17. zero: (0, 0, 0),
  18. negative: (-5, -2, -3),
  19. large_numbers: (999999, 444444, 555555)
  20. },
  21. {
  22. println!("a={}, b={}, expected={}", a, b, expected);
  23. }
  24. }
  25. fn main() {
  26. positive();
  27. negative();
  28. }

If I change fn $name_\$test_name() to fn $test_name() , it works well.

So I want to know how to concatenate the two variables $name $test_name to output one string. For example, if $name is foo and $test_name is bar, how to output foo_bar?

答案1

得分: 1

以下是翻译好的部分:

"paste" 是一个crate called paste ,它提供了一个用于在声明性宏中连接标记的宏。

要使用它,将整个结果包装在 paste! 宏中,然后通过将它们放在 [<>] 之间并用空格分隔来连接标记。

  1. use paste::paste;
  2. #[macro_export]
  3. macro_rules! parameterize {
  4. ($name:ident, $params:pat, {$($test_name:ident : ($($args:expr),*)),*}, $test_body:block) => {
  5. paste! {
  6. $(
  7. fn [< $name _ $test_name >]() { // &lt;- paste! 使用的特殊语法!
  8. let $params = ($($args),*);
  9. $test_body
  10. }
  11. )*
  12. }
  13. };
  14. }

使用您提供的其余代码,这将生成函数 should_be_added_positiveshould_be_added_negative

英文:

There is a crate called paste, that provides a macro for concatenating tokens inside declarative macros.

To use it, wrap your entire result in the paste! macro, and then you can concatenate tokens by placing them between [&lt; and &gt;] with spaces to separate.

  1. use paste::paste;
  2. #[macro_export]
  3. macro_rules! parameterize {
  4. ($name:ident, $params:pat, {$($test_name:ident : ($($args:expr),*)),*}, $test_body:block) =&gt; {
  5. paste! {
  6. $(
  7. fn [&lt;$name _ $test_name&gt;]() { // &lt;- special syntax used by paste!
  8. let $params = ($($args),*);
  9. $test_body
  10. }
  11. )*
  12. }
  13. };
  14. }

Using the remainder of the code you've provided, this would generate functions should_be_added_positive and should_be_added_negative.

huangapple
  • 本文由 发表于 2023年2月19日 10:18:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497611.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定