是否可以将元组解包为函数参数? [英] Is it possible to unpack a tuple into function arguments?
本文介绍了是否可以将元组解包为函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想解压一个元组并将其作为参数传递,有没有办法做到这一点:
If I want to unpack a tuple and pass it as arguments is there a way to do this:
//Does not compile
fn main() {
let tuple = (10, Vec::new());
foo(tuple);
}
fn foo(a: i32, b: Vec<i32>) {
//Does stuff.
}
而不必这样做:
fn main() {
let tuple = (10, Vec::new());
foo(tuple.0, tuple.1);
}
fn foo(a: i32, b: Vec<i32>) {
//Does stuff.
}
推荐答案
在夜间编译器上:
#![feature(fn_traits)]
fn main() {
let tuple = (10, Vec::new());
std::ops::Fn::call(&foo, tuple);
}
fn foo(a: i32, b: Vec<i32>) {
}
AFAIK 没有稳定的方法可以做到这一点.
There is AFAIK no stable way to do that.
这篇关于是否可以将元组解包为函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文