Rust 如何使用宏将元组扩展到其成员作为函数参数?

Rust 如何使用宏将元组扩展到其成员作为函数参数?,rust,macros,Rust,Macros,我有一个来自外部库[1]的函数,如 fn set_color(r: f64, g: f64:, b: f64) 我想在元组中管理我的颜色,如 let yellow = (1., 1., 0.); 我想到了一个宏,比如: macro_rules! rgb { ( $rgb:expr ) => { rgb.0, rgb.1, rgb.2 } } 然后 set_color(rgb!(yellow)); 不幸的是,Rust接着说:错误:宏扩展忽略标记“,”和任何后续内容 我怎样才

我有一个来自外部库[1]的函数,如

fn set_color(r: f64, g: f64:, b: f64)
我想在元组中管理我的颜色,如

let yellow = (1., 1., 0.);
我想到了一个宏,比如:

macro_rules! rgb {
    ( $rgb:expr ) => { rgb.0, rgb.1, rgb.2 }
}

然后

set_color(rgb!(yellow));
不幸的是,Rust接着说:
错误:宏扩展忽略标记“,”和任何后续内容

我怎样才能做到这一点



[1] :
cairo::Context::set_source_rgb()
和friends

您不能这样做。Rust宏不是执行哑文本操作的C宏;Rust宏必须生成有效的Rust代码,
a、b、c
无效

最接近的方法是将函数传递给宏:

macro_rules! rgb {
    ($f:expr, $rgb:expr) => {
        $f($rgb.0, $rgb.1, $rgb.2)
    };
}

出于好奇-为什么需要一个宏?一个函数可以满足吗
fn rgb(color\u tup:(f64,f64,f64))
?@chub500如何使用
rgb
函数调用提供的
set\u color
函数?可能我缺少了一些东西:
set\u color(color\u tup.0,color\u tup.1,color\u tup.2)?@chub500据我所知,关键是OP不想对每个函数调用进行元组解包。如果它只是一个函数,我可以将其包装。但是
cairo::
有很多类似的功能。
let white = (1., 1., 1.);
rgb!(set_color, white);