Rust: Using tuples as arguments

Created on 15 Sep 2014  路  5Comments  路  Source: rust-lang/rust

In rust we have possibility to return several values in tuple. But we can't use it vice versa: to put several arguments to function in tuple. Like this:

fn main() {
    sum(prepare_args());
}

fn sum(x: int, y: int) -> int {
    x + y
}

fn prepare_args () -> (int, int) {
    (1, 2)
}

I think it will be very usefull.

Most helpful comment

This is already possible, just not automatically like your snippet suggests:

fn sum((x, y): (int, int)) -> int {

All 5 comments

This is already possible, just not automatically like your snippet suggests:

fn sum((x, y): (int, int)) -> int {

You could go the apply route a la lisp:

fn apply<A,B,C>(f: |A,B|->C, t: (A,B)) -> C {
    let (a,b) = t;
    f(a,b)
}

fn main() {
    apply(sum, prepare_args());
}

I believe you should be able to mimic Scala's tupled w/ unboxed closures, but I'm not sure if that's possible yet.

Variadic generics adds a similar feature to Rust, but with a more explicit syntax: foo(..(1, 2, 3)) == foo(1, 2, 3). Unfortunately, we鈥檙e probably not going to get variadic generics (at least not before 1.0).

"Before 1.0" isn't relevant; Rust is on a rolling release model.

A language change like this should be proposed through the RFC process:

https://github.com/rust-lang/rfcs

An issue like this isn't actionable because a clear design and consensus to implement it are required before making language changes.

Was this page helpful?
0 / 5 - 0 ratings