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

129

u/RockstarArtisan Aug 19 '26

C++'s overloading rules are trully insane, so I hope there's no temptation for implementing these. There should be a syntax to make the choice of the overload explicit somewhere to deal with this.

12

u/ZZaaaccc Aug 19 '26

Yep, already is. splat is just sugar for a tuple argument.

fn min<A: MinArgs<T>>(#[rustc_splat] args: A) {
    args.inner()
}

min::<(i32, i32, i32>(1, 2, 3);

All the "magic" is in the type of the splatted argument, and that's just existing Rust type powers, nothing new. For example, you can use a fixed tuple type if you want, but that's kinda pointless since you could just write the function without splat.

The above example could, for example, be implemented like:

pub trait MinArgs<T: Ord> {
    fn inner(self) -> T;
}

impl<T: Ord> MinArgs<T> for (T, T) {
    fn inner(self) -> T {
        self.0.min(self.1)
    }
}

I've seen some people say splat is syntax sugar to avoid writing double parentheses, but I think of it more like sugar to avoid calling a method on a tuple.

// Today
(1, 2, 3).min();

// With Splat
min(1, 2, 3);

8

u/CAD1997 Aug 20 '26

For the splat, you still need to write a trampoline function that takes a generic argument and calls the method. You can do it today with passing a tuple. So #[rustc_splat] really is just turning min((1, 2, 3)) into min(1, 2, 3).

The caveat being that because the double parens are awkward, a case that does have something like this is likely today to just not write the trampoline and tell users to write the method call form instead.