August 29th, 2024

From Julia to Rust

The article outlines the author's transition from Julia to Rust, highlighting Rust's memory safety features, design philosophies, and providing resources for learning, while comparing code examples to illustrate syntax differences.

Read original articleLink Icon
From Julia to Rust

The article discusses the author's journey from learning Julia to exploring Rust, highlighting the differences in philosophy and design between the two programming languages. The author emphasizes the importance of Rust in addressing memory safety issues prevalent in large tech corporations, citing talks that illustrate the urgency of adopting safer programming practices. Rust's ownership model and borrow checker are presented as solutions to common pitfalls in systems programming, contrasting with Julia's focus on performance through multiple dispatch and JIT compilation. The author provides a brief historical context for both languages, noting that Julia emerged from frustrations with existing scientific computing tools, while Rust was developed to improve productivity in low-level systems programming. The article also includes a list of resources for learning Rust, emphasizing the need for a structured approach to grasp its complexities, such as generics and traits. The author concludes by comparing code examples in both languages, illustrating the additional boilerplate required in Rust for achieving similar functionality to Julia, which is more concise and flexible. Overall, the piece serves as a guide for Julians interested in transitioning to Rust, outlining the benefits and challenges of the shift.

- The author is transitioning from Julia to Rust, motivated by Rust's memory safety features.

- Rust's ownership model and borrow checker help prevent common programming errors.

- The article provides historical context for both languages and their design philosophies.

- A list of resources for learning Rust is included, emphasizing structured learning.

- Code examples illustrate the differences in syntax and complexity between Julia and Rust.

Link Icon 10 comments
By @miguelraz - 3 months
Author here!

I have no idea why this blog is making the rounds again and I've learned a lot of Rust since, see my (badly benchmarked!) presentation at this year's Scientific Computing in Rust 2024:

https://www.youtube.com/watch?v=bdsBhqKOe7A

and a PR I sent in for the Rust compiler to speed it up for some low hanging fruit:

https://github.com/rust-lang/rust/pull/110477

I'll have to revisit this blog at some point and see what I should update, as well as a "From Rust to Julia" post for those that want to venture into that world as well.

By @nritchie - 3 months
I'm a physicist whose written substantial projects in both Julia and Rust (and C/C++/Object Pascal/Java/Python/...). I'm also a person who refactors frequently. While my mind was initially blown away by all the cleverness in Julia, I eventually found that for non-trivial packages the "time-to-first-X" became painful - way more painful than compiling Rust code (mostly with debug occasionally with --release). Plus the amazing generality of Julia code was both a blessing and a curse. Julia really needs interfaces to ensure that a duck really is a duck. In the end, I find Rust (despite/maybe because of) the picky compiler to be overall much more productive. With Rust, I find I can make major refactors, when the code compiles and my test code runs, I'm good to go. With Julia, months after I've refactored, I'm still finding footguns. I still use both but it is Rust that has my heart these days.

Other thoughts: Developing Rust on Windows is painful but on Linux a joy. both cargo and Julia package managers are wonderful compared to other language alternatives.

By @exo-cortex - 3 months
Just one thing (maybe the author reads this):

The nicest way to avoid the "oh a lot of boilerplate!"-problem with implementing "Add" for a custom type is by using the "derive-more"-crate. Then you simply put a "#[derive(Add)" above your struct. That was the initial reason I switched to Rust myself: because I was writing a dynamical systems solver in C++ and had to overload arithmetic operators for my state-struct. But doing this was so complicated and I never felt sure of it performance wise (what if one side is a reference? And so on).

In rust I have a macro and don't have to ever worry about that again.

By @dist1ll - 3 months
> For loops are not idiomatic in Rust

That's not really the case. For loops are certainly less common compared to C, but they are completely fine to use. Forcing every loop into an iterator-chain style can sometimes be more confusing.

Not to mention doing for-iterations with early exits. Yes, you can use try_fold, but if I'm not writing generic iterator library code, I'd prefer explicit for in cases like this.

> writing Fortran-ey code instead of iterators will lead to pain and slower loops.

No it won't. At least not in the general case. Not to mention that iterators also have performance footguns that are pretty easy to trigger.

By @djhn - 3 months
Really enjoyed this article, even though I’ve only ever explored the basics of Julia. I’m always on the lookout for articles and guides that would help people with basic R, Julia and Python REPL skills to learn software engineering patterns, computer science basics or, especially in the case of R, more general purpose languages.
By @a-dub - 3 months
interesting question: does it still make sense to have different or specialized languages for scientific computing vs generalized software engineering?
By @Decabytes - 3 months
I'm still not understanding the switch to Rust other than for education (which is fine). All of the things you get from Rust with memory safety you get in Julia with the Garbage Collector?
By @tiffanyh - 3 months
Maybe the worst TLDR ever :)

Interesting post, even though I don’t understand “Why Rust” because all the reasons cited are not an issue with Julia (eg memory safety).

This seems to not be about Julia at all. It just a post of a person who only knew Julia, and is now wanting to learn another language (which happens to be Rust)

By @hi-v-rocknroll - 3 months
In the "and Rust can be used for X" department:

Add new builtins to Bash using a shared library (or .bundle on macOS) written in Rust. Bash also allows running bash other commands from native code. (There's a rust crate for creating bash builtins, but it doesn't currently compile on mac due to some improper and some missing linker flags.)