Search code examples
rusttuples

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.
}

Instead of having to do this:

fn main() {
    let tuple = (10, Vec::new());
    foo(tuple.0, tuple.1);
}
fn foo(a: i32, b: Vec<i32>) {
    //Does stuff.
}

Solution

  • On a nightly compiler:

    #![feature(fn_traits)]
    
    fn main() {
        let tuple = (10, Vec::new());
        std::ops::Fn::call(&foo, tuple);
    }
    fn foo(a: i32, b: Vec<i32>) {
    }
    

    There is AFAIK no stable way to do that.