• 5C5C5C@programming.dev
    link
    fedilink
    arrow-up
    3
    ·
    2 days ago

    There are several ways to achieve an effect equivalent to operator overloading in Rust, depending on exactly how you want the overloading to work.

    The most common is

    fn do_something(arg: impl Into<Args>) {
        ...
    }
    

    This lets you pass in anything into the function that can be converted into the Args type. If you define the Args type yourself then you can also define any conversion that you want, and you can make any construction method you want for it. It’s a small touch more explicit than C++'s operator overloading, but I think it pays off overall because you know exactly what function implementation all different choices of arguments will be funneling into.

    I’ll admit there’s one thing from C++ that I frequently wish were available in Rust: specialization. Generics in Rust aren’t exactly the same as templates in C++ but they’re close enough that the concept of specialization could apply to traits and generics. There is ongoing work to bring specialization into the language, but it’s taking a long time, and one of my projects in particular would seriously benefit from them being available.

    Still, Rust will have specialization support long before C++ has caught up to even a quarter of the benefits that Rust has over it.