# Comparison of Rust to Julia for scientific computing?

**URL:** <https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508>\
**Category:** Offtopic\
**Tags:** question, rust\
**Created:** [March 26, 2022, 11:47am UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508 "2022-03-26T11:47:18Z")\
**Posts on this page:** 20\
**Page:** 6

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 21, 2023, 4:29pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/101 "2023-03-21T16:29:51Z")

</div>

> [@miguelraz](#):
>
> `Julia + Rust`

reinventing two-language problem?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [March 21, 2023, 5:04pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/102 "2023-03-21T17:04:11Z")

</div>

It’s the two language solution 🥳

---

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [March 21, 2023, 6:28pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/103 "2023-03-21T18:28:11Z")

</div>

Super interested! Would be curious to see what you’ve been learning in the Rust world. I always thought of Julia as the language you reach for for multi-purpose programming (e.g. from reading a CSV into a DataFrame for analysis to making fun random animations 😉 ) whereas Rust you’d want for critical infrastructure or command line tools. Curious if you will have any perspectives on how Julia might compare now in the CLI space due to 1.9’s incoming improvements to caching and precompilation.

Hope to see you at JuliaCon soon!!!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 21, 2023, 6:37pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/104 "2023-03-21T18:37:23Z")

</div>

Rust is on the other side a bit way too safe for me:

```rust
fn main() {
    let a = [1, 2, 4];
    // Sum of the squares of all of the elements of the array
    let mut sum_sq = 1.0;
    for x in a.iter() {
        sum_sq *= x;
    }
    println!("sum_sq = {}", sum_sq)
}

```

errors with

```julia
error[E0277]: cannot multiply-assign `{float}` by `&{integer}`
 --> src/main.rs:6:16
  |
6 | sum_sq *= x;
  | ^^ no implementation for `{float} *= &{integer}`
  |

```

I do understand the motivation for that, but having to do explicit type conversions even to multiply a float by an integer to store the result in a previously defined float starts to become too cumbersome.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 21, 2023, 7:12pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/105 "2023-03-21T19:12:19Z")

</div>

> [@gbaraldi](#):
>
> It’s the two language solution 🥳

that’s a registered trade mark by X + Python

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 21, 2023, 9:22pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/106 "2023-03-21T21:22:54Z")

</div>

Completely agree that numeric conversion papercuts for people trying STEM stuff in Rust are capable of hemorrhaging the joy of computing out of anyone.

One of my many gripes in Rust land (and boy do I have a few, in particular to the technical computing aspects) is that you have to rethink from loops to iterators to solve your problems.  
This isn’t bad per se but does have a learning curve and (potentially) amazing parallelism payoffs.  
I would rewrite your code as

```rust
fn main() {
     let a = [1,2, 4];
     let res: i32 = a.iter().map(|e| e*e).sum();
     println!("{res}");
}

```

This idiom is required because for loops with normal indexing are subject to bounds checking by default, which is likely to kill SIMD and other optimizations.  
The added benefit is that you can make this entire loop parallel (and data-race free) with the `rayon` crate and by just doing `.iter() -> .par_iter()`.

…and I find that sort of approach powerful.

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 21, 2023, 9:26pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/107 "2023-03-21T21:26:39Z")

</div>

and if you’re wondering why this says `.map(|e| e*e)` and not `.map(|e| e^2)` it’s because `^` in Rust is bitwise xor which is… painful.

Math does not usually look like math in Rust and that’s one of the many pain points of why I don’t think Rust is (yet) a good fit for technical computing.

We may still have to contend with it as “user friendly Fortran with scant package management woes”, but do come to the BoF for more in-depth discussion and spicier takes 🌶

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [March 21, 2023, 9:55pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/108 "2023-03-21T21:55:59Z")

</div>

> [@miguelraz](#):
>
> `let res: i32 = a.iter().map(|e| e*e).sum()`

That starts to look like `numpy` 🙄

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [March 22, 2023, 12:26am UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/109 "2023-03-22T00:26:46Z")

</div>

I’ve spent the last 6 months casually learning about Rust. I think the popularity behind Rust is really driven by how it’s revolutionized putting safe memory management in the hands of users. As some here have pointed out, to some degree this is simply making it more difficult to do anything without actively thinking about safety. I assume we’ll see the pendulum swing the other way on some of that because it goes past helping developers be safe to getting in the way of knowledgeable developers that just want to get something done.

I’ve also looked into the traits thing for Rust and there’s nothing especially magical about it. The ability to inherit/propagate traits to new types has a specific mechanism under the hood (the [derive attribute](https://doc.rust-lang.org/book/appendix-03-derivable-traits.html)). We could implement things like this in Julia but it involves additional compilation steps.

Clearly interactive code is better for scientific computing in many instances and I don’t see how Rust could ever accomplish this without radically changing its entire approach (only compile code that can be proven safe). It’s kind of like asserting that Julia should be used as the primary tool in constructing an operating system. Both could be possible eventually but you’re looking at years of work outside of the current purview.

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [March 22, 2023, 3:01am UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/110 "2023-03-22T03:01:51Z")

</div>

Rust is wonderful, but sometimes it takes too much workaround to get things down. For example, I can not serialize `Box<dyn MyTrait>`, with `MyTrait` implemented some types with generic parameters `impl<T> MyTrait for MyStruct<T>`. To get to that, I have to use `enum` or `Box` everywhere, then in each implementation add `#[typetag]`. That make the codes very complicated and limited.  
What made me to learn Rust is the problem of memory fragmentation, when I run the Julia code in Jupyter again and again, the program became slower and slower. But recently I found out that Rust has the same problem.  
But in my personal view, although writing Julia code is more enjoyful than Rust, but Rust is really powerful in some other aspects. It has a bigger community than Julia, it has wonderful toolkits such as rust analyzer, which make refactoring easy, I don’t have to take much care to write high performance code.  
What I said above is just my personal narrow view, I just learned Julia for 15 months and Rust for 3 months, so don’t take that seriouly.

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 22, 2023, 2:21pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/111 "2023-03-22T14:21:30Z")

</div>

I’m afraid the notation was lifted almost directly from Ruby (which some early Rustaceans were close to in the early days:

```ruby
a = [1,2,4]
b = a.map{|x| x * x}.sum

```

---

<div class="post-metadata">

**Author:** ![Topherc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/topherc/32/47836_2.png) [@Topherc](https://discourse.julialang.org/u/Topherc)\
**Post date:** [March 22, 2023, 11:30pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/112 "2023-03-22T23:30:49Z")

</div>

Ahh, you’re right here. So with Julia, string indexing must be the same internally as Rust’s `.chars().nth(idx).unwrap()`. The Rust code is more verbose, less convenient/obvious, but more explicit. In a way this tiny example reflects the broader differences in experiences these languages bring to the developer. I find Rust to have a steeper learning curve than other modern languages, and at least for me it takes a little longer to develop in. I do love Rust. There are some really great things about it, but there are definitely trade-offs.

For scientific computing, you usually want your focus to be on the computing so-to-speak, not on object lifetimes, stack vs heap, and the other nitty-gritties that Rust demands your awareness of. A garbage collector is a wonderful thing if you don’t mind a very small runtime penalty, less fine-grained control over memory management, and slightly bigger binaries.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [March 23, 2023, 12:27am UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/113 "2023-03-23T00:27:39Z")

</div>

Rust and Julia are both building on LLVM and if you lurk some forums you can see Rust is pushing up against some issues in LLVMs memory model which I anticipate being incorporated in future llvm releases. So I don’t think it’s an all or none thing here but you’re looking at some aspects of Rust that are pushing up against what it’s toolset can do (and ours).

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [March 23, 2023, 6:02pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/114 "2023-03-23T18:02:50Z")

</div>

Stefan Gränitz is mentoring a GSoC for optimizing those specific parts of the LLVM/Rust infra if people are interested in speeding that up

> **[LLVM Projects in 2023 Google Summer of Code](https://weliveindetail.github.io/blog/post/2023/03/20/llvm-gsoc-2023.html)**
>
> LLVM has issued a number of challenging project ideas for the upcoming GSoC. The application period for contributors starts today, 20 March and ends on 4 April.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [March 24, 2023, 4:18pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/115 "2023-03-24T16:18:17Z")

</div>

If you’re really interested in memory management and safety in Julia this seems like the perfect time to start learning the internals and create a formal proposal of how Julia could adopt some of these advances into a formal API.

---

<div class="post-metadata">

**Author:** ![AMJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amj/32/214096_2.png) [@AMJ](https://discourse.julialang.org/u/AMJ)\
**Post date:** [March 24, 2023, 5:47pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/116 "2023-03-24T17:47:27Z")

</div>

A good example is [Bumper.jl](https://github.com/MasonProtter/Bumper.jl/). It is a proof of concept but pretty cool package. If I had the know-how knowledge, I would have delved in since I really like to see more packages like this on semi-manual memory management.

---

<div class="post-metadata">

**Author:** ![Zach\_Christensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zach_christensen/32/7220_2.png) [@Zach\_Christensen](https://discourse.julialang.org/u/Zach_Christensen)\
**Post date:** [March 25, 2023, 2:25pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/117 "2023-03-25T14:25:45Z")

</div>

That’s probably the sort of conceptualization that we need for a proposal at this point. It doesn’t need to be optimized or even fully safe. Just something given proper native support could be made optimized and safe. That would handle half the work, naming stuff and formalizing a hierarchy of rules.

---

<div class="post-metadata">

**Author:** ![FrancisKing](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/francisking/32/24966_2.png) [@FrancisKing](https://discourse.julialang.org/u/FrancisKing)\
**Post date:** [April 17, 2023, 7:08pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/118 "2023-04-17T19:08:08Z")

</div>

This code doesn’t do sum of squares.

---

<div class="post-metadata">

**Author:** ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)\
**Post date:** [May 25, 2023, 3:50pm UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/119 "2023-05-25T15:50:01Z")

</div>

Heads up, people here might be interested in the ScientificComputing in Rust 2023

> **[Scientific Computing in Rust 2023](https://scientificcomputing.rs/)**
>
> The webpages for the Scientific Computing in Rust 2023 workshop

See you there!

---

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [May 26, 2023, 7:51am UTC](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508/120 "2023-05-26T07:51:26Z")

</div>

Rust repl evcxr is nice, but is fay away behind IJulia. And repl is really import for scientific computing.

[Previous page](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508.md?page=5)

[Next page](https://discourse.julialang.org/t/comparison-of-rust-to-julia-for-scientific-computing/78508.md?page=7)
