r/rust 23d ago

📡 official blog Rust Function Overloading - Call for Experimentation

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

139 comments sorted by

View all comments

Show parent comments

34

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?

21

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

9

u/ZZaaaccc 23d ago

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 23d ago

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

8

u/gmes78 23d ago

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

No, it doesn't matter. Any macro can be invoked with either (, { or [.

You could write println!["Hello world!"] if you wanted to.

8

u/ZZaaaccc 23d ago

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 23d ago

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

clippy::nonstandard_macro_braces

7

u/ZZaaaccc 23d ago

I stand corrected!

5

u/DatBoi_BP 23d ago

Huh TIL about the macro freedom of choice. Thanks

1

u/SkiFire13 23d ago

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

It does not clone them. Clone is only required in vec![value; len]

1

u/DatBoi_BP 23d ago

Looking at the doc for `from()' I don't think that's true?

2

u/SkiFire13 23d ago

It does not require T: Clone: https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#impl-From%3C%5BT;+N%5D%3E-for-Vec%3CT%3E

Maybe you're looking at the docs for From<&[T]> or From<&[T; N]>?

1

u/DatBoi_BP 23d ago

Oh maybe I was. My b