Don't write Rust like it's Java
The author discusses transitioning from Java to Rust, highlighting Rust's type safety, differences in traits and interfaces, complexities of ownership, and the importance of embracing Rust's unique features for effective programming.
Read original articleThe author reflects on their experience transitioning from Java to Rust, emphasizing the importance of adapting to Rust's unique features rather than trying to force it into a Java-like structure. They appreciate Rust's type and memory safety, which can catch many errors at compile time that would otherwise occur in dynamic languages like Python. However, they caution against treating Rust as a direct replacement for Java, particularly in terms of object-oriented programming. The author discusses the differences between Java interfaces and Rust traits, noting that while traits serve a similar purpose, they require different handling, such as boxing or using generics. They also highlight the complexities of ownership and lifetimes in Rust, suggesting that sometimes using functions instead of service objects can simplify code. Ultimately, the author encourages embracing Rust's idioms and paradigms to fully leverage its capabilities, rather than imposing familiar patterns from other languages.
- Rust offers strong type and memory safety, catching many errors at compile time.
- Rust's traits differ from Java's interfaces, requiring different handling techniques.
- Ownership and lifetimes in Rust can complicate service dependencies.
- Using functions instead of service objects can simplify Rust code.
- Embracing Rust's unique features is essential for effective programming in the language.
Related
My experience crafting an interpreter with Rust (2021)
Manuel Cerón details creating an interpreter with Rust, transitioning from Clojure. Leveraging Rust's safety features, he faced challenges with closures and classes, optimizing code for performance while balancing safety.
Behavior Inheritance in Rust (2021)
Rust favors composition over inheritance due to limited support for traditional inheritance. To mimic behavior inheritance, developers can use a P-Impl pattern, adding a base object field and implementing a trait to access it efficiently.
I Probably Hate Writing Code in Your Favorite Language
The author critiques popular programming languages like Python and Java, favoring Elixir and Haskell for immutability and functional programming benefits. They emphasize personal language preferences for hobby projects, not sparking conflict.
I Hope Rust Does Not Oxidize Everything
The author expresses concerns about Rust's widespread adoption in programming, citing issues with syntax, async features, complexity, and long compile times. They advocate for language diversity to prevent monoculture, contrasting Rust with their language Yao.
Investing in Rust
Investing in Rust programming language can enhance cybersecurity by preventing memory-related vulnerabilities. Challenges in adoption include integration issues and skill set mismatches, suggesting U.S. policy interventions for promotion.
If you try to program Rust like C you are going to fight and lose.
If you try to program Rudt like and Object Oriented Language you will eventually go mad.
If you like a Zen master let all previous knowledge and pre-conceived notions of what beautiful code has to be out of the window and do it the Rust way, suddenly things flow and stop being a pain.
And that experience will help you be a better programmer, period.
This is not at all the reason. The real reason is that Rust chooses to make the runtime overhead required for dynamic typing and heap allocation explicit, not anything to do with memory safety.
Java puts everything into the heap and creates easy expressability by having pointers everywhere. In contrast, Rust often avoids pointers, prefers the stack and is very stingy with memory access.
These languages are completely orthogonal to each other and it has consequences in their usage. Not that a good chunk of people wouldn’t butcher the code they are given in any codebase…
I’m not sure if I would recommend it if you’re working as part of a team though, other java devs will hate your code lol.
An enum often suffices to express the different combinations.
Of course, from a pattern perspective, that is "not extensible" but it often ends up being easier to maintain and in Rust, often compiles down to more efficient code.
Don't write [insert lang here] like it's Java.
I've seen this in Ruby code bases, TS code bases, even Erlang code bases (using gen servers like an OO primative.)
Java makes this king of separation between data and logic really hard.
Kotlin came to the rescue for us. It allows us to move our Java/OO codebase in a slightly more FP direction: uncouple data and logic, immutability is preferred, no (or very little: yes looking a you shitty "platform types" in Kotlin) implicit nulls.
Discussing curriculum and textbooks for a Java class today, someone recommended a book that was most recently updated for Java 7. When I pointed that out, the response was:
“The level we teach at, the new Java features won’t make much of a difference.”
Java is a culture just as much as a language, and the culture is stuck somewhere in the past in a lot of places, especially in education.
I suspect the author might be reading this post back and cringe a bit. That last code listing with handle_session_completed triggers my inner clippy. Why clone session.customer_id to then pass it as a reference? Why are those CheckoutSession fields Options?
The other advice I would give is to identify what the author calls "Service" as what is called "view type" or "newtypes" in rust. A newtype wraps another type, to give it a different set of methods. For example, a counter could wrap an i32 and only allow incrementing and reading the value. Or put together a set of references to fields/slices of a struct.
It can be useful, but the way it's used by the author here is not very rusty, and the final suggestion of using a function is a descent alternative. Although I think defining handle_session_completed as a method on CheckoutSession or UserRepo might be better.
Related
My experience crafting an interpreter with Rust (2021)
Manuel Cerón details creating an interpreter with Rust, transitioning from Clojure. Leveraging Rust's safety features, he faced challenges with closures and classes, optimizing code for performance while balancing safety.
Behavior Inheritance in Rust (2021)
Rust favors composition over inheritance due to limited support for traditional inheritance. To mimic behavior inheritance, developers can use a P-Impl pattern, adding a base object field and implementing a trait to access it efficiently.
I Probably Hate Writing Code in Your Favorite Language
The author critiques popular programming languages like Python and Java, favoring Elixir and Haskell for immutability and functional programming benefits. They emphasize personal language preferences for hobby projects, not sparking conflict.
I Hope Rust Does Not Oxidize Everything
The author expresses concerns about Rust's widespread adoption in programming, citing issues with syntax, async features, complexity, and long compile times. They advocate for language diversity to prevent monoculture, contrasting Rust with their language Yao.
Investing in Rust
Investing in Rust programming language can enhance cybersecurity by preventing memory-related vulnerabilities. Challenges in adoption include integration issues and skill set mismatches, suggesting U.S. policy interventions for promotion.