3 ms·
> Strictly speaking, Rust doesn't support overloaded functions. Function overloading is when you define the same function but with different arguments, and the
by demurgos 1y ago
> Strictly speaking, Rust doesn't support overloaded functions. Function overloading is when you define the same function but with different arguments, and the language selects the correct one based on the argument type. In this case, it's two different implementations of a trait for two different types.
You're right that there is no function overload in this article, just some implicit derefs.
I would argue however that Rust has overloaded functions (function resolution based on argument types) due to how it handles trait resolution with implicit inference. Rust may not have syntax sugar to easily define overloads and people generally try to avoid them, but using argument-based dispatch is extremely common. The most famous example is probably `MyType::from(...)`, but any single-method generic trait using the generics for the method arguments is equivalent to function overloading. There are also other techniques. Using nightly features you can get far enough so a consumer can use native function call syntax.
Overload on nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=239218207a2f7e8d50491c10bfb92fd0 https://play.rust-lang.org/?version=nightly&mode=debug&editi...
Overload on stable: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4bc63996f33ca91d555ceffa65f5ad91 https://play.rust-lang.org/?version=stable&mode=debug&editio...
The mechanism and syntax may be different from overloading in C++ or Java, but as a user the result is the same and it causes the same pain points.
- steveklabnik 1y ago> The mechanism and syntax may be different from overloading That's all I'm saying. > as a user the result is the same That's why I said I didn't think the inaccuracy is the worst detail.