On 07/09/13 22:11, Renato Lenzi wrote:
Is it possibile, or will be possibile, to use default parameters and/or named parameters in Rust?
In the docs i couldn't find anything about this (may be my fault...)
Regards.

It is not possible currently, but there's an open bug [6973] and with (quite) a bit of discussion. Two current methods for simulating them would be defining wrapper functions for the common shortcuts, or passing the default/named args as a struct and use a "functional record update" (FRU), e.g.

  struct FooArgs {
     x: int,
     y: int,
     z: int,
  }

  static DEFAULT_FOO: FooArgs = FooArgs { x: 1, y: 2, z: 3 };

  /// a, b are compulsory, anything in rest is a default arg
  fn foo(a: uint, b: uint, rest: FooArgs) { ... }


  fn main() {
    // only defaults
    foo(10, 100, DEFAULT_FOO);

    // y = -3, defaults for the rest
foo(10, 100, FooArgs { y: -3, .. DEFAULT_FOO }) // `..` indicates an FRU
  }

This one is unfortunately ugly, but it seems to get the job done.

Huon

[6973]: https://github.com/mozilla/rust/issues/6973
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to