Skip to content

Variadic functions (varargs) #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mahkoh opened this issue Mar 10, 2015 · 3 comments
Closed

Variadic functions (varargs) #960

mahkoh opened this issue Mar 10, 2015 · 3 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Mar 10, 2015

Some food for thought:

fn f(args: ..i32) {
    // args has an anonymous type which implements Iterator<Item=i32>
    for arg in args {
        println!("{}", arg);
    }
}

fn g(args: ..&Any) {
    // args implements Iterator<Item=&Any>
    for (i, arg) in args.enumerate() {
        if arg.is::<&str> {
            println!("{}: &str: {}", i, arg.downcast_ref::<&str>().unwrap());
        } else if arg.is::<&i32> {
            println!("{}: i32: {}", i, arg.downcast_ref::<i32>().unwrap());
        }
    }
}

fn main() {
    f(1, 2, 3, 4);
    // 1
    // 2
    // 3
    // 4

    g(&"hello", &"world", &1, &2);
    // 0: &str: hello
    // 1: &str: world
    // 2: i32: 1
    // 3: i32: 2

    // Splice syntax
    let vec = vec!(1, 2, 3, 4);
    f(..vec.into_iter());
    // 1
    // 2
    // 3
    // 4
}
@glaebhoerl
Copy link
Contributor

(See also #376.)

@mbrubeck
Copy link
Contributor

See also #2137.

@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Feb 23, 2018
@cramertj
Copy link
Member

Closing since I think #2137 covers this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

5 participants