Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macros 可以使用宏展开为构造函数的元组吗?_Macros_Rust - Fatal编程技术网

Macros 可以使用宏展开为构造函数的元组吗?

Macros 可以使用宏展开为构造函数的元组吗?,macros,rust,Macros,Rust,给定以下元组赋值: let (a, b, c, d) = (Item::new(1), Item::new(10), Item::new(100), Item::new(1000)); 可以将其简化,以便可以删除构造函数并将其生成宏。e、 g: let (a, b, c, d) = item_tuple!(1, 10, 100, 1000); 从递归宏来看,似乎每个宏实例化都需要创建一个有效的元组,因此宏将创建元组对,例如:let(a,(b,(c,d))=() 是否可以编写一个扩展为构造函数

给定以下元组赋值:

let (a, b, c, d) = (Item::new(1), Item::new(10), Item::new(100), Item::new(1000));
可以将其简化,以便可以删除构造函数并将其生成宏。e、 g:

let (a, b, c, d) = item_tuple!(1, 10, 100, 1000);
从递归宏来看,似乎每个宏实例化都需要创建一个有效的元组,因此宏将创建元组对,例如:
let(a,(b,(c,d))=()


是否可以编写一个扩展为构造函数平面元组的宏?

您可以在宏中接受可变参数,并通过调用
Item::new()
将其展开,如下所示:

macro_rules! item_tuple {
    ($($arg:expr),*) => {
        (
            $(Item::new($arg),)*
        )
    }
}
使用此宏,此调用将按预期工作和行为:

let (a, b, c, d) = item_tuple!(1, 10, 100, 1000);