Collect variadic arguments
Mark the final parameter name: ...Type to collect surplus arguments into a fresh array. A call may supply none. The fixed part of the signature still has normal arity.
Variadic functions are called directly. They cannot be stored as function values, passed by name, or fed an existing array with spread syntax.
fn sum(values: ...int) -> int {
let mut total = 0
for value in values { total += value }
total
}
fn main() {
println(sum())
println(sum(1, 2, 3, 4))
}
Examples use the moon toolchain version shown beside each result.
Output & diagnostics
Expected: 0 10
Ctrl/⌘+Enter runs the current lesson.