HiveBrain v1.2.0
Get Started
← Back to all entries
patternrustMajor

Is it possible to unpack a tuple into function arguments?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
unpackpossiblefunctiontupleintoarguments

Problem

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) {
    //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) {
    //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) {
}


There is AFAIK no stable way to do that.

Code Snippets

#![feature(fn_traits)]

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

Context

Stack Overflow Q#39878382, score: 51

Revisions (0)

No revisions yet.