How does Rust make me write good code?

175 points manypeople1account

I don't know how, somehow Rust encourages me to write better structured code.

I don't fully understand how.. I tried "avoiding side effects" in other languages, but hasn't been enough. Rust seems to give me nudges to structure code in ways that I would have never done in other languages.

Java is "strongly typed", yet I don't feel nudges from java to structure my code better. Java permits you to use UnsignedInteger types for example, but people usually don't use it.. Rather, building classes feels like a hassle with lots of boilerplate.

Rust is unique in forcing you to only having one mutable reference at a time, but I doubt this alone makes you build great code. Languages which which avoid this by lacking references, don't make me write great code.

I do notice Rust is completely unambiguous, which helps me think about certain optimizations. But still, that is not enough to make great code.

It seems like a combination of multiple such attributes makes Rust nudge the user towards better code. I have realized when coding in other languages, I sort of "hack it till it works." Test in runtime, fix any bugs along the way. Rust seems to give me an alternative option to cover various edge cases, without needing to run them.

It has now gotten me to a point where, when I get an assignment at work, I've been able to first write it up in Rust, and make sure the compiler is happy - I don't even need to run the code.. just compiling should enough - then translate the code into my target language. This way I avoid the whole "hack it till it works" part, because it just works..