Python to Rust Translator?


#1

Has anyone heard any mutterings of a translator from Python to Rust?

I have not done much Rust programming, and the most extensive was a translation from Python to Rust. It was a program that did lots of list manipulations and crunching data. But were I to really throw a lot of data at it, it would have been too slow. Looking at how my Python program was wasting time I concluded it was taking time because it was Python; I was only wasting time in the one place where I knew I have been inefficient.

My translation to Rust was designed to speed things up.

But in the process I also discovered a pair of complementary bugs that had been hidden in the Python code (unintended sharing of a list, that a second bug quietly depended upon).

I also discovered that the Rust version was line-by-line analogous to the Python. Side-by-side it looked like a demented Python. Seems to beg for an automated translation, at least for part of the work.

Python, by being so dang dynamic, is a great prototyping language: it doesn’t just defer types until its run time, it allows me to defer some of those decisions, too. But once the prototype is working, in most cases the types are not ranging all over the place, they are pretty well behaved. Just not specified. It would be nice to get some help rewriting it in Rust, to pin down those latent types, to get Rust’s speed and correctness.

It is possible that Python’s dynamic nature makes it really hard to even begin to pin down types. Okay, maybe involve the programmer: ask. It doesn’t have to be a turn-key solution to be useful, an interactive translator would be useful, too. Or maybe cython-like annotations…

Make sense?

Thanks,

-kb


#2

You might be using Python in a very specific way that happens to translate well. In my experience translating Python -> Rust, things end up drastically different.

I haven’t heard of any py2rust effort yet, but it would be fantastic if someone did it! :smile:

I imagine such an effort would be fantastically difficult, in part because both languages tend to rely heavily on 3rd-Party modules for specific functionality.


#3

That is true. In my case I was doing pretty much pure data in, crunch on it in Python, data out.

Something else I am working on now is Python, simple data slopping and marshaling, etc., with the only “special” library I am using being Ceph object store RADOS library. Not only is there a Rust version, at first glance it is more complete than is the Python library. (Still the APIs are not something a translator will know how to translate between.)

About the time I have figured out the exact behavior I want for this current project, the Python will be working but too slow, I’ll then want it to be fast and safe, and expect to translate it to Rust. By hand, alas.

But maybe someday…

-kb


#4

Or, I maybe was writing Python in Rust. Might be stylistically ugly, but still fast and safe.

-kb


#5

it would be fantastic if someone did [py2rust]!
I imagine such an effort would be fantastically difficult.

Hello all. This is my first post here, and I am complete newbie to rust.

I am the author of Leo. My first python program was something that turned into Leo’s c-to-python command. Search for c-to-python in the file, or better yet, look at the file in Leo…

The c-to-python command converts c or c++ text to something closer to python text. It is purely a text-based munging of the original text. So the conversion isn’t anywhere near perfect, but it eliminates a lot of tedious text futzing.

A py2rust.py script with similar limited goals would be feasible. It would also help bring me up to speed on rust syntax :wink: Anyone interested?

Edward


#6

A major issue I faced when translating a Go library to Rust was that “supposed” non-overlapping array access by indices was heavily used(e.g. using both a[0] and a[1] mutably in Go, but you can’t do this in Rust cause a will be borrowed mutably as a whole yadadada).

I ended up doing the translation twice to get it right.

I suspect this, or some similar issues, will be a major roadblock toward fully automatic translation, but a syntactic translation is still helpful maybe.


#7

If someone wants to try a Python2Rust I suggest to start with Python code that’s fully type annotated with MyPy. This simplifies the problem (otherwise you’re trying to do what ShedSkin used to try to do with Python and C++).