This is just a little fun project and should probably not be used in production. Currently variadics are only implemented up to a dimension of 4
. Variadic
's are only pssible because of default types and I consider this implementaion a hack
.
Variadic<T>
is the trait that makes variadic arguments possible in stable Rust. VarArgs1
, VarArgs2
etc implement Variadic<T>
which allows the user the call pop()
.
pop
will return the first argument inside an Option
and another VarArgs(n-1)
. For example
~~~
let (Some(value), rest: VarArgs2
A simple sum example implemented with recursion.
~~~
fn sum
Here we call pop
on VarArgsN<i32...>
and it will return (Option<i32>, VarArgs(N-1)<i32...>)
. The recursion stops at VarArgs0
which will returns a (Option<i32>, VarArgs0<i32>)
where Option<i32>
will always be None
.
~~~
fn fact
It is also possible to use traits with Variadic
. Here we constrain T
with std::fmt::Debug
, then we print out every value that we pop
off until we reach VarArgs0
.
~~~
fn debugprint
debug_print(VarArgs3(1, 2, 3)); ~~~