Python to Rust conversion. Rust in general

I have a heap of python code gstreamer glib stuff I’m wanting to port to rust?
Is there a converter to port python to rust known or in development you know of??

Rust in general.

I’ve been slowly learning rust and already purchased a few books with examples etc.
I recently gave a talk yo my radio club on rust where I declared rust will be then new replacement for C and C++ in future. Hope I’m not disappointed. Covered the enhancements from C and C++. Many where interested on what rust was about and was well received with examples explained about mutable, borrow checker and enum, struct etc.

I’m so happy to see a language which respects the underlaying hardware memory stack,heap and security.
For many years, programming has become sloppy relying on raw cpu grunt, hard disk size and memory since it all cheap.

I remember the days of programming is Z80 assembler pulling together a word processor in under 16K??

What happened.

1 Like

You might enjoy searching for available crates using lib.rs maintained by @kornel, which has some ergonomic search features compared to crates.io.

A search for "python parser" yielded an option called python-parser. Without having used the crate, and with no shade to the developer of the crate, I am skeptical that "a heap of code" can or should be ported to Rust without being thoughtfully refactored. Good luck tho!

1 Like

A "heap" suggests a lot of lines of code to me.

In general I would advise against going to the trouble of rewriting a lot of code just because it is desired to use a different language. If it works it works. What is the actual practical reason to rewrite it?

I suspect automatic translation of Python to Rust is not a realistic possibility. I have yet to see such a translator that does not make a complete mess of code as output. Even if it does work one would not want to maintain it.

3 Likes

Maybe start by profiling which parts of the code are slow and/or potentially unsound and start from there, using e.g. pyo3 to expose those parts to Python? Seems more manageable, less risky, and higher cost-benefit than trying to port "a heap" of Python without a meaning or benefit.

2 Likes

This is the way. Don't rewrite code in Rust just for the sake of rewriting code in Rust.

Instead, use Rust's awesome FFI abilities to call that other language's code from your Rust. Then add value by building things on top of the existing code, adding new functionality, or rewriting just the slow/error-prone code in Rust.

Embrace. Extend. Extinguish.

That said, @SonnyWalkman you may want to make sure your excitement and enthusiasm don't get the better of you. If not managed correctly, you can come across as a fanatic and push people away, or you might fall in love with the language so much you don't realise when it isn't the right tool for the job.

... Of course, if this is all for a personal project then ignore everything I said and have at it. Manually rewriting code from one language to another is an excellent way to learn the language and see how they compare.

6 Likes

ZiCog,
Thanks for the advice.
The python code works as intended however performance issues is the only issue.
It’s a personal project gstreamer pipelines with some sockets to communicate with other code.
Most of the gstreamer stuff already had rust bindings so should be straight forward. It’s the other management code which is the dream of porting over.
No clients to be hurt, just time and coffee.

Sonny

FYI, if you do need Python FFI, PyO3 (pyo3) is what I've heard of.

I worry that in attempting to "port" a large piece of Python or other language to Rust one might get frustrated when finding the the style the code is written in is not so easily achieved in Rust. For example something that relied on lots of OOP in inheritance might be tricky to simply "port" to Rust.

I put "port" in quotes here because the word implies to me expecting a simple, almost mechanical, line for line, translation to Rust. Rather I would think in terms of rewriting and perhaps redesigning. Using the original code as a high level specification

.

3 Likes