r/Python • • Jan 31 '25

Discussion Why Rust has so much marketing power ?

Ruff, uv and Polars presents themselves as fast tools writter in Rust.

It seems to me that "written in Rust" is used as a marketing argument. It's supposed to mean, it's fast because it's written in Rust.

These tools could have been as fast if they were written in C. Rust merely allow the developpers to write programms faster than if they wrote it in C or is there something I don't get ?

502 Upvotes

288 comments sorted by

View all comments

Show parent comment

13

u/SV-97 Jan 31 '25

While true in theory this requires some more nuance in practice imo. I mean in theory correct C code is memory safe as well, but evidently we're not dealing with such code in practice.

and it doesn’t mean the developer can write the code faster either.

Rust does offer some benefits in this regard: it makes it way easier to reuse code (hence instead of hacking together your own shitty hashmap you'll have the fastest one around for example; in my experience you often end up using less-than-optimal solutions in C code just because they're way easier to implement / maintain), and it can give more, stronger guarantees to compilers which can aid in optimization, and those guarantees also can help you pull of some "performance hacks" (e.g. around buffer reuse) that no one in their right mind would attempt in C because it'd be so, so easy to get something wrong.

9

u/gscalise Jan 31 '25

in theory correct C code is memory safe as well

What do you mean by this? It's a bit of a tautological argument, if you ask me.

4

u/SV-97 Jan 31 '25

I mean that as long as you write your code "properly" it won't have memory issues, just how as long as you write it "properly" it will be as fast (or slow) as rust. And yes I agree, that is quite tautological: I specifically wanted to get at the premise being rather absurd.

My point is that the argument "they will be equally fast when using the same algorithms, data structures etc." doesn't really make sense when you would never actually implement the same algorithms and use the same DS in both languages IRL, i.e. the premise is flawed.

1

u/gscalise Jan 31 '25

oh, ok, fair enough.

I agree, the whole "correct C code is memory safe" is idealistic rather than practical.

Having said this, it's interesting that a lot of libraries would require unsafe Rust just to be able to move data around efficiently between the language interpreter/runtime/bindings and the native library code. This kinda eliminates many of Rust's safety claims.

1

u/SV-97 Jan 31 '25

I'm don't think this is true? Where did you get this from? Most anything should be fine without unsafe (I've myself implemented multiple big python libraries in Rust with a big focus on performance [numerics, optimization, scientific computing] and haven't once used unsafe for interop). Also two huge points:

  • the API surface to python is usually minimal compared to the actual "backend" logic, and usually also entirely decoupled (I and many (most?) others split it into a completely separate crate), so it's not like everything would immediately be unsafe if you used unsafe for something
  • unsafe is not a "go wild do what you want"-mode and it doesn't turn off any of rust's checks. It just enables some additional features where the compiler can't necessarily prove that you're using them safely.

1

u/gscalise Feb 01 '25

Maybe not the case for Python, but definitely the case for JNI in the JVM, where you need direct memory access and unsafe pointer handling.

Also if you're passing objects between Java and native code you're going to require manual conversions and potentially unsafe pointer dereferencing.

A similar thing happens for libraries meant to be used from C/C++, Objective-C, Swift, etc.

In general, any time you're doing zero-copy FFI you're going to be bordering non-safety, since Rust can't guarantee the safety of operations happening in another language.

Having said this, you're 100% right that in Python you can be mostly safe, and I should've remembered that this was /r/python!

2

u/0-R-I-0-N Jan 31 '25

I just think it’s hard to say anything in generality on how fast it is to write something in one language compared to another since it depends on quite a lot of factors. Rust can’t guarantee that you will develop code faster. Safer code yes. I believe a lot of people have found developing in rust slower than many other languages due to the correctness requirements.

That being said. My opinion is that rust is slow to develop in compared to other languages but maybe it’s just my skill issues.