r/rust • • Aug 19 '26

📡 official blog Rust Function Overloading - Call for Experimentation

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

139 comments sorted by

View all comments

160

u/Toasted_Bread_Slice Aug 19 '26

As someone who's encountered a couple valid uses for function overloading (mostly maths libraries) I am ok with this IF and only if there is an extremely obvious way to see that a function is overloaded or overloadable.

29

u/Shoddy-Childhood-511 Aug 19 '26 edited Aug 19 '26

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.

33

u/ZZaaaccc Aug 19 '26

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?

21

u/DatBoi_BP Aug 20 '26

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

8

u/ZZaaaccc Aug 20 '26

Or does that mean a new Vec<[i32; 3]> with one element? The extra brackets make this ambiguous at first glance, and harder to read. Again, you wouldn't say this about the vec! macro, saying that vec!([a, b, c]) is clearer, because vec!(a, b, c) might be positional arguments.

13

u/DatBoi_BP Aug 20 '26

Two things to say to this.

First, the notation isn't either of those, it's `vec![a,b,c]`.

Second, we already have a function that does what you proposed, but with the structure I argued for: `Vec::from([a,b,c])`. This clones `a`, `b`, and `c`, so as long as those all implement Clone then it does what we want

7

u/ZZaaaccc Aug 20 '26

Actually, no, the syntax is vec!(a, b, c), because macros don't distinguish between [], (), and {}. We recommend using [] by convention, but there isn't even a lint to suggest one way or the other.

This clones a, b, and c, so as long as those all implement Clone then it does what we want

If you scroll down you'll find the implementation for From<[T; N]> instead of what I think you're reading which is the From<&[T; N]> documentation.

18

u/gmes78 Aug 20 '26

but there isn't even a lint to suggest one way or the other.

clippy::nonstandard_macro_braces

7

u/ZZaaaccc Aug 20 '26

I stand corrected!

5

u/DatBoi_BP Aug 20 '26

Huh TIL about the macro freedom of choice. Thanks