upvote
I beliebe q3k's comment should be read as "[even if it's acceptable to the most stringent of gatekeepers] then this would be churn for the sake of churn."'

Not that only idiomatic Rust is appropriate.

reply
I get the feeling, and shooting for idiomatic on a rewrite is definitely wrong.

That being said, "idiomatic" is more just saying "clean and familiar". It's using the right language features in the right places.

For example, you could write something like this

    fn add_double(a: f64, b: f64) -> f64 {
      return a + b;
    }

    fn add_float(a: f32, b: f32) -> f32 {
      return a + b;
    }
But that's not idiomatic. Idiomatic would look something like this

    fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
      return a + b;
    }
The benefit of the idiomatic approach is now you have a function which handles a bunch of types from u32, to f64 and it also handles custom types and traits which implement the add ops.

The first method is what you might write if you were, for example, translating from C to Rust. It isn't idiomatic but it's easy to do.

The other thing to realize is that compiler authors optimize for idiomatic. The more you do things in a strange fashion, the more likely you are to stumble over a way of writing code which isn't being looked at when the language team is looking at performance and compile time optimizations.

There's nothing wrong with non-idiomatic code per say. However, part of learning a language is learning the idioms. It makes you better at that language.

reply
Pedantic:

    fn add<T: std::ops::Add<Output = T>>(a: T, b: T) -> T {
      a + b
    }
reply
Not really. Rust is designed to be written in a certain way. If you machine translate C into Rust you end up with a load of `unsafe` code that follows the C style but consequently doesn't get any of the benefits of being written in Rust.

Imagine if you translated assembly to C++, but you just did it by putting everything in `asm("...")` calls. That's not idiomatic C++ and you wouldn't get any of the benefits of using C++.

That said, the Rust code I skimmed actually did look surprisingly idiomatic. It wasn't full of `unsafe` like I would have expected.

reply