Experimenting Rust with Python

Learnin Rust, one step at a time
rust
Published

November 11, 2023

2023-11-11

As a Python only developer, I found it extremly hard to learn a new langauge particular with language like C++ or Rust that involves build steps. (Does it has debugger support like PyCharm?). I tried to learn Rust by reading the book last year, I finished a few chapters but never get to write any programme and I forgot most of it already. The only thing that stays is Rust has a concept of lifetime and borrow checker.

I am hoping that by pushing my learning journey online will give myself extra motivation. Ultimately I want to learn just enough Rust to optimise performance for Python program, it could be just simple algorithm, or if I ever learn enough I would like to write an event simulation engine (or at least learn to build one).

Key learning today: - pyo3 seems to be the choice for Rust binding for Python. I checked what ruff used (An insanely fast Python linter and now formatter). I prefer to stick with a good enough choice (noted that building and distributing Python package is a headache, I don’t want to spend my energy on this now) - I followed the PyO3 tutorial to build a Rust binding for Python. - I try to follow as much as possible without forcing myself to understand everything. Building something functional first and try to break it is a better learning process for me. - Use of #[pymodule] and maturin develop to build Python package. - You can also run Python in Rust!

It build and install the package string_sum.

>>> import string_sum
>>> dir(string_sum)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'string_sum', 'sum_as_string']

Questions - string_sum.cpython-310-darwin.so, this is the only file I find in the site_package alongside __init__.py. How do I find the interface other than doing dir?