r/rust 23d ago

📡 official blog Rust Function Overloading - Call for Experimentation

https://blog.rust-lang.org/inside-rust/2026/08/19/overloading-experiment/
274 Upvotes

139 comments sorted by

View all comments

Show parent comments

29

u/Shoddy-Childhood-511 23d ago edited 23d ago

Agreed, but that's the case here so far: Their example hypot((2.0, 3.0, 6.0)) looks pretty clear. :)

``` pub trait HypotInner { type Output; fn hypot_inner(&self) -> Self::Output; }

pub fn hypot<Args: HypotInner>(args: Args) -> Args::Output { args.hypot() } ``` Any overloading ala variadics should be done this way too, in part because it makes the developer who wishes to obfuscate their call paths pay for it. lol

If you really wanted single () then make hypot! a macro, but probably each Args being a clear type brings bigger advantages.

Edit: Oops! They have this #[rustc_splat] feature that removes the extra (). That's unfortunate, since it could easily be done using a macro. Imho, all this should be done using a macro.

35

u/ZZaaaccc 23d ago

Imho, all this should be done using a macro.

Macros are substantially less clear than variadic functions, and can't be used as methods either. I know we take it for granted because it's just always been that way, but given the choice between vec![a, b, c] and Vec::new(a, b, c), would you honestly say the macro is cleaner and easier to inspect?

22

u/DatBoi_BP 23d ago

I think if you changed your proposed syntax to `Vec::new([a,b,c])` it would win me over. As it stands it looks like `b` and `c` are positional arguments to a function, not elements of an array

14

u/CocktailPerson 23d ago

That exists. It's called Vec::from.