Comment by tialaramex

3 years ago

All of Rust's functions are available as "free-standing functions" because of how types work in Rust, and Rust doesn't have classes, just structs. So, as a consumer nothing changes.

   if std::collections::HashSet::is_empty(&basket) {

... is perfectly legal Rust, it would just be more idiomatic to write:

   if basket.is_empty() {

The availability of all functions as "free functions" is convenient when you need, say, a filter predicate, since of course std::collections::HashSet::is_empty is exactly what you wanted if what you wanted to express was the predicate "is this HashSet empty?" and in languages that aren't allowed to do this you'd need to pointlessly shuffle chairs around to achieve the same thing instead.

Yup, that's a good point, UFCS means that every method/function is free-standing =)

  • Universal Function Call Syntax goes both ways, Rust only offers to treat all of the things that seem like "methods" as free functions but not the reverse.

    Under UFCS I could extend your Rust type by simply writing a new free function which matches the type, and this is in fact deliberately forbidden. (I can write the free function, but, doing so does not extend the type and I can't call it using the "method" syntax).