[rust-dev] Polymorphism & default parameters in rust

Emmanuel Surleau emmanuel.surleau at gmail.com
Thu Aug 2 12:51:01 PDT 2012


Hi,

I'm new to rust, and I'm struggling to find an elegant way to work with default
parameters.

I'm trying to implement in rust the equivalent of the following Python line:

def flag(self, name, desc, short_name=None, max_count=1, banner=None):
    ...

The idea is to offer a "simple" API (with only name and desc mandatory, while
making available number of advanced options if needed).

My first move was to try polymorphism:

fn a(x: ~str) -> ~str {
    #fmt("First function with %s", x)
}

fn a(x: ~str, y: ~str) -> ~str {
    #fmt("Second function with %s and %s", x, y)
}

fn main() {
    #info("Result: ");
}

Oddly enough, this compiles, but the first function is shadowed by the second,
so attempting a(~"something") does not compile. I assume this is not an intended
behaviour.

Further research hasn't turned any solution. Is there some idiomatic way in rust
of having defaults for parameters?

Cheers,

Emm


More information about the Rust-dev mailing list