What can Rust do that Python can't?

I am just curious to know the capabilities that Rust can provide that Python can't so I can chose the right language for whatever task may come ahead?

A quick answer is that Rust lets you program at a much lower level than Python, and thus expressing more precisely how your program should operate while maintaining the memory safety guarantees that a language like Python provides. The result is that you can write programs that typically run much more quickly than Python while still having confidence that the program will run as expected.

Whether Rust is beneficial for your task will likely depend on the nature of the task. At a high level what is your project?

5 Likes

Does Rust also generate a much more efficient binary compared to Python (when used as a compiled language)?

I see, does Python come with a GC?


It could be anything but for now I am going to be using Processing and some Audio libraries that can take various information from an audio file such as Tempo, pitch/key, loudness of the audio etc and then to generate some shapes based on that. Python also has support for the Processing library as well.

I tried to look for these audio libraries for Processing but only thing I could find was how to get the loudness at a current point of the audio. I can't find something that deals with tempo and pitch/key.

Rust does seem to have some processing crate and some rust-portaudio but I am not sure if it offers what I am looking for. I am not too sure if there are other audio related crates/libraries out there that can easily in a high level fashion give me those kinds of details?

2 Likes

Here's a really rough comparison of the two languages...

Rust:

  • Code is compiled to native code (this allows you to write code that is as fast as possible)
  • Statically typed (you need to be able to determine every variable's type just by looking at the source code)
  • Memory is managed by libraries and types instead of the runtime, using patterns like RAII and Drop to make sure it is freed deterministically whenever the object goes out of scope
  • Calling into other native code (FFI) is zero-cost (although unsafe)
  • Language promotes correctness and handling errors properly

Python:

  • Code is interpreted by the Python virtual machine (the VM has inherent overhead and things requiring high performance are often done by calling into libraries written in C/C++/Rust)
  • Dynamically typed (variables can have any type at runtime, and can often change type over time)
  • Garbage collected
  • FFI is possible, but must be done using things like ctypes or cffi
  • Language promotes rapid prototyping and iteration
7 Likes

With Rust you can write operating system, capture packets within the kernal, rewrite existing C library without breaking change, and be called by mobile apps. But most importantly, you can write a Python interpreter in Rust without relying to existing implementation.

4 Likes

I thought Python also can be used as a compiled language from what I heard?

The most common Python implementation, cpython is a bytecode interpreter.

You may be referring to numba, a library which lets you annotate a piece of code with a special @jit annotation. This annotation then uses the Python runtime to deconstruct the function and just-in-time compile it to native code that will then be called by the runtime.

1 Like

That is not true or at least massively misleading. If you are writing idiomatic Rust, it's very rare that you need explicit deallocation. The point of the ownership model and RAII is exactly that dellocation happens at the appropriate time automatically, preventing both memory leaks and use-after-free.

3 Likes

Thanks for pointing that out. I wasn't sure how to say Rust does manual memory management, but uses things like RAII and Drop so you don't have to remember to explicitly release memory after you're done with it... I've updated the original comment, but let me know if there's a better way to word it which is understandable for beginners.

3 Likes

Rust would be a great language to write an audio processing library in and create bindings to it for Python, Node, …

It doesn't really work as a scripting language.

2 Likes

I see, thanks man.

Does it already exist?

The other way python can be "compiled" makes a single executable for your script that ends up bundling an entire python installation in the executable that effectively gets unzipped at runtime. Not surprisingly this is not very efficient.

2 Likes

The other way to compile Python code into a binary is to use something like Nuitka. As far as I remember, it transpiles Python to C and then compiles. But sometimes simialr projects have some restrictions, and forbid using certain libraries or language features.

2 Likes

Rust can be used as a high level language, but it's strong point is efficiency in terms of running fast, not using a lot of memory, having small executables, but at the same time being able to do this safely, or at least "unsafe" bits of code are minimised and well isolated. I think of it as being like "C", but with a safety harness.

Here is a video which explains some of the projects Rust is being used for: https://www.youtube.com/watch?v=MgV9KWt-0SY

2 Likes

Thanks for the link :slight_smile:

Does Rust also have support for audio processing, for example using in a high level fashion to get the tempo of a song, the pitch/key signature and the loudness of the audio?

I don't know any language which would have such support. However, any language, including Rust, allows one to write libraries supporting any high-level operation (I'm explicitly excluding things like system calls or raw assembly).

2 Likes

The question is: Does a crate exist that implements these features. To answer that, search on crates.io or lib.rs: https://lib.rs/search?q=pitch

1 Like

In fact, all programming languages are in this sense equally powerful: In the 1930’s, Alonzo Church and Alan Turing both demonstrated that a program written in one language can always be translated into any other (subject to a few assumptions). So instead of asking about what a programming language can do, it’s usually more enlightening to look at questions like:

  • How easy is it to express various ideas?
  • What reusable modules have already been written?
  • What performance characteristics does the resulting executable have?
2 Likes

Typically languages do not have support for audio processing and such things. The only one I know is BASIC that has a "BEEP" keyword built right into it's syntax. I guess there may be a domain specific language for audio-processing.

Generally such functionality is written in libraries/modules/packages that one can use from your program and share with others. I'm pretty sure that if you find an audio library for Python that does what you describe it will actually be written in C or C++ to get the performance required. Rust would make a great alternative there.

I gather from your repeated references to audio processing that this thread is nothing to do with Rust vs Python but rather how to make use of some simple to use audio library in whatever language.

For detecting pitch/frequencies in a sound source one might use the Fourier Transform which for sure is available in Rust. For example: https://docs.rs/fourier/0.1.0/fourier/

Of course you may need to get the sound live from some sound device: https://docs.rs/soundio/0.2.1/soundio/

Whether anyone has package all this kind of thing into a dead easy package for Rust your might want to use I have no idea.

4 Likes