Rust seems to have two kinds of functions. I can write either f64::sin(x)
or x.sin()
. However, if I define a freestanding function fn f(x: f64, ...)
I cannot invoke f
with x.f(...)
. On the other hand, different types can have methods with the same name, but I cannot overload freestanding functions (and have no desire to). I can sort of make new postfix functions that by creating a trait and implementing it for f64
(or whatever the type of the first argument is) and this seems to be a well-known pattern, especially to make more methods on iterators or Result
objects.
I am not proposing an alternative but I'd like to understand why traits are the only way to make new postfix functions. Are there technical reasons why (like something would break) or perhaps taste? (The fact that you have to jump through hoops to make postfix functions suggests that you should do it sparingly.)