# Blog post: Rust vs Julia in scientific computing

**URL:** <https://discourse.julialang.org/t/blog-post-rust-vs-julia-in-scientific-computing/101711>\
**Category:** Offtopic\
**Tags:** rust\
**Created:** [July 17, 2023, 10:17pm UTC](https://discourse.julialang.org/t/blog-post-rust-vs-julia-in-scientific-computing/101711 "2023-07-17T22:17:20Z")\
**Posts on this page:** 1\
**Showing post:** 96

<div class="post-metadata">

**Author:** ![Mo8it](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mo8it/32/32649_2.png) [@Mo8it](https://discourse.julialang.org/u/Mo8it)\
**Post date:** [July 20, 2023, 8:14pm UTC](https://discourse.julialang.org/t/blog-post-rust-vs-julia-in-scientific-computing/101711/96 "2023-07-20T20:14:25Z")

</div>

I did update the blog post to address some issues.

# Undefined behavior

Most importantly, I finally answer why I did use a “straw man example” as described here:

> [@brianguenter](#):
>
> Your case will be stronger if you don’t use straw man examples such as this one
> 
> ```julia-auto
> v = Vec{Int64}(undef, 3)
> 
> ```
> 
> which you say causes reliability problems because of the undefined elements. But you could have done this instead:
> 
> ```julia-auto
> v = Int64[]
> sizehint!(v,3)
> 
> ```

Benny had to highlight this in a very **kind** way:

> [@Benny](#):
>
> In Julia, you would simply make a vector with 0 length, there’s no need to preallocate if you don’t have values yet. If you want to increase its allocated capacity without changing its semantic length, so that planned `push!`es don’t need to allocate more than once overall, you can use `sizehint!`. As I said before, solid background research is important if your goal is to compare how two tools address the same problems. You could instead promote Rust without comparison, there’s no need to make unhelpful basic errors.

I answer it in the following **new section in the post about “Preallocation and undefined behavior”** :

[https://mo8it.com/blog/rust-vs-julia/#preallocation-and-undefined-behavior](https://mo8it.com/blog/rust-vs-julia/#preallocation-and-undefined-behavior)

* * *

## Correction about having to use a `for` loop

> [@sdanisch](#):
>
> I haven’t benchmarked Rust against Julia a lot, but I’d be surprised to see large differences between Julia’s iterators and Rusts… Maybe you run into a performance bugs in some specific implementation, or was this just an assumption without ever benchmarking it?

I did say that you have to write a `for` loop for maximum performance in Julia but I did mean vectorization. It was unfortunate that I did mention this without enough explanation and directly after talking about Rust’s iterators. This did lead to the confusion that I meant Julia’s iterators.

This has been a problem I did observe 2 years ago. It is also mentioned [here](https://discourse.julialang.org/t/when-should-i-write-loops-or-vectorised-calls/50812) and [here](https://mo8it.com/blog/rust-vs-julia/#preallocation-and-undefined-behavior). But now I did benchmark vectorized and `for` loop versions and it seems to be fixed? I did remove that now. Sorry.

Sorry for the incorrect benchmarking that I did publish today for some minutes until @DNF pointed out that I did benchmark two different things. I did take the whole website down until the corrected version was uploaded. I have no intention to spread wrong information. I made a mistake and apologize to the whole Julia community ❤

* * *

> [@martin.d.maas](#):
>
> Maybe you don’t need to pre-allocate in Rust

You have to preallocate in Rust as in any other language and it offers many ways to do so. For example, `Vec` has the methods `with_capacity`, `reserve`, `reserve_exact`. You could also use `extend` which will extend from an iterator while preallocating based on the `size_hint` of the iterator.

> [@martin.d.maas](#):
>
> Other points of relevance that haven’t been mentioned: how mature is GPU computing in Rust? For what I see [here](https://rust-gpu.github.io/Rust-CUDA/features.html) not so much. And arguably the GPU has and will have a growing importance in scientific computing, so Rust seems to be really lagging in a critical area.

Yes, you are right. This was indeed a big point in the conference. We need something similar to `rayon` but for the GPU.

> [@Tamas\_Papp](#):
>
> This was a very interesting dicussion, but for me it mostly highlights how broad the term _scientific computing_ is: it involves all kinds of programs with different constraints and trade-offs

Yes! I think we have to emphasize this more in this discussion 😃

* * *

@mbauman @Mason @martin.d.maas @xiaoxi @jakobnissen Thank you very much for your kind words. It means a lot to me ❤

---

_[View the full topic](https://discourse.julialang.org/t/blog-post-rust-vs-julia-in-scientific-computing/101711)._
