# Proof of concept: variadics in pure rust using existing nightly features

**URL:** <https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258>\
**Category:** Uncategorized\
**Created:** [July 26, 2024, 6:56pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258 "2024-07-26T18:56:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 26, 2024, 6:56pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/1 "2024-07-26T18:56:30Z")

</div>

[playground](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=823b864d82e17bce267d3eb11785639e)

if you did some trickery with existential type aliases to name a specific function's type, you could probably do this with actual functions or even closures.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [July 26, 2024, 6:59pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/2 "2024-07-26T18:59:35Z")

</div>

This feature is called overloading, not variadics. Variadics are a way to write functions that take any number of inputs, while in this case you would have to write an `impl` for each number and at some point you'll have to stop (which is what many projects already do).

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 26, 2024, 7:11pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/3 "2024-07-26T19:11:06Z")

</div>

true, although if we ever get variadic tuples this will become a viable option.

and technically speaking ISO C only mandates that compilers are able to handle functions (including variadic functions) with up to 127 arguments, which isn't entirely unreasonable to brute-force.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [July 26, 2024, 8:40pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/4 "2024-07-26T20:40:41Z")

</div>

> [@binarycat](#):
>
> true, although if we ever get variadic tuples this will become a viable option.

I'm not sure I understand for what would this be a viable option. If we get variadic tuples the we already get variadics, no?

> [@binarycat](#):
>
> and technically speaking ISO C only mandates that compilers are able to handle functions (including variadic functions) with up to 127 arguments, which isn't entirely unreasonable to brute-force.

Even with a limit at 128, doing this library wise is going to be too slow for practical uses, see for example `diesel`'s `128-column-table`.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 26, 2024, 9:20pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/5 "2024-07-26T21:20:36Z")

</div>

> [@SkiFire13](#):
>
> I'm not sure I understand for what would this be a viable option. If we get variadic tuples the we already get variadics, no?

variadic tuples are not _necessarily_ the same as true variadic functions. rust actually already supports calling variadic functions, but they must be defined in another language like C.

---

<div class="post-metadata">

**Author:** ![160644346](https://avatars.discourse-cdn.com/v4/letter/1/7bcc69/32.png) [@160644346](https://internals.rust-lang.org/u/160644346)\
**Post date:** [July 26, 2024, 9:39pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/6 "2024-07-26T21:39:34Z")

</div>

Even without nightly features you can somewhat make variadics

```rust
fn variadic(args: Vec<&str>) {
   // …
}

```

and call it like this

```rust
variadic(vec!["hi", "hello"])

```

---

<div class="post-metadata">

**Author:** ![HjVT](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/hjvt/32/10365_2.png) [@HjVT](https://internals.rust-lang.org/u/HjVT)\
**Post date:** [July 27, 2024, 12:26am UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/7 "2024-07-27T00:26:29Z")

</div>

Given that `Fn` traits use tuples to represent arguments, that really seems like the same thing.

What support there is for variadics, exists only for cffi compatibility.

---

<div class="post-metadata">

**Author:** ![binarycat](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/binarycat/32/12210_2.png) [@binarycat](https://internals.rust-lang.org/u/binarycat)\
**Post date:** [July 27, 2024, 9:34pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/8 "2024-07-27T21:34:31Z")

</div>

well, variadic function pointers are kinda neat, i suppose.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [July 29, 2024, 9:00am UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/9 "2024-07-29T09:00:03Z")

</div>

> [@160644346](#):
>
> ```rust
> fn variadic(args: Vec<&str>) {
> // …
> }
> 
> ```

I'd expect it to look like this (without allocation):

```rust
fn variadic<const N: usize>(args: [&str; N])

```

But true variadics could also handle different types:

```rust
fn variadic(args: ...(impl ToString))

```

---

<div class="post-metadata">

**Author:** ![idanarye](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/idanarye/32/3346_2.png) [@idanarye](https://internals.rust-lang.org/u/idanarye)\
**Post date:** [July 29, 2024, 8:01pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/10 "2024-07-29T20:01:41Z")

</div>

There is room for both kinds of variadics. D, for example, has both:

[https://onecompiler.com/d/42mmdnjxt](https://onecompiler.com/d/42mmdnjxt)

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 27, 2024, 8:01pm UTC](https://internals.rust-lang.org/t/proof-of-concept-variadics-in-pure-rust-using-existing-nightly-features/21258/11 "2024-10-27T20:01:43Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
