# 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:** 25

<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 18, 2023, 3:54pm UTC](https://discourse.julialang.org/t/blog-post-rust-vs-julia-in-scientific-computing/101711/25 "2023-07-18T15:54:21Z")

</div>

> [@algunion](#):
>
> Now, if you are aware of the Julia ecosystem, you might use JET

Thank you for indirectly assuming that I am not aware of the Julia ecosystem. I actually am, but you did not read the whole blog post and did the cherry picking that you criticize 🙂 Because I have an [appendix about `JET.jl`](https://mo8it.com/blog/rust-vs-julia/#jet-jl)

It is nice that you can get a bit closer to the Rust version in Julia. But you are missing the point of having proper [sum types](https://en.wikipedia.org/wiki/Tagged_union) as first class citizens in a language that tells you what a function exactly returns in its signature.

> [@algunion](#):
>
> Also - presenting an example like the usage of `Vector{Any}` as some performance foot gun is not a good idea: `Vector{Any}` can work wonders in part of the code where the performance is irrelevant (thus Julia’s flexibility).

I think that although `Any` provides flexibility in dynamic languages, collections of `Any` is a big mistake in a language focused on Performance.

Rust has enums which are **sum types** that can provide you with that flexibility:

```rust
enum Color {
    Gray(u8),
    RGB(u8, u8, u8),
    RGBA(u8, u8, u8, u8),
    Named(String),
}

// `colors` has the type Vec<Color>
let colors = vec![
    Color::Gray(42),
    Color::RGB(10, 0, 20),
    Color::RGBA(0, 0, 255, 100),
    Color::Named(String::from("red")),
];

```

There are also [trait objects](https://doc.rust-lang.org/book/ch17-02-trait-objects.html) which you can use if you only care about the structs implementing some behavior (trait).

> [@algunion](#):
>
> People will not use `Vector{Any}` inside some tight loop where the performance is crucial: and instead of writing `v1 = []`, they would use `v1 = Int[]` (or whatever type is appropriate in the context).

This is what I teach in my Julia courses, but it still happens that a vector does not end up with a concrete type that you expect and Julia will not even warn you about it. This is what I did mean with " Have fun profiling, using the macro [`@code_warntype`](https://docs.julialang.org/en/v1/manual/performance-tips/#man-code-warntype) while interactively calling a function, etc."

I think that Julia should have taken the approach of Rust of deriving the type of the vector without even having to specify it like in the following example:

```rust
// The type of the vector is not known yet, but it will be derived.
let mut v = Vec::new();

// We push a float with 64 bits, therefore v has the type Vec<f64>.
v.push(1.0);

// The type will not change!
// The line below will give a compile error because we are pushing an integer into a vector of floats.
// v.push(1);

```

You can specify variable types in Rust, but you don’t have to in most cases.

---

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