"重载"搜索结果 1 条

Rust为什么不支持函数重载?

函数重载和 Rust 的 trait 本质上都是 Ad-hoc polymorphism 的一种表现形式,两者表达能力基本相同,比如如下 C++ 中的函数重载int foo(int); int foo(int, double);在 Rust 中可以用 Trait + Tuple + Generic 模拟 trait FooImpl { fn foo_impl(self) -> i32; } impl FooImpl for (i32,) { fn foo_impl(self) -> i32 { .. } } impl FooImpl for (i32, f64) { fn foo_impl(self) -> i32 { .. } } fn foo(f: F) -> i3…