Skip to content

KuchikiRenji/Rewriting-a-Python-Script-in-Rust

Repository files navigation

Rewriting a Python Script in Rust | Python to Rust Migration Example

Migrate Python data-processing code to Rust for better performance. This repository demonstrates a full Python-to-Rust rewrite: same logic (factorial), testing, linting, and benchmarking—useful for learning Rust migration and comparing Python vs Rust speed.


What This Project Does

This project rewrites an existing Python script in Rust and compares performance. It is a practical example of:

  • Code migration — Translating a Python data-processing script into idiomatic Rust while keeping behavior identical.
  • Performance comparison — Measuring execution speed and resource usage of the Rust version vs the Python version.

Use this repo to learn how to rewrite Python in Rust, see Rust vs Python performance on a small workload, and follow a Python to Rust migration workflow with tests and tooling.


Quick Start

Prerequisites: Rust (and optionally Python 3 for the original script).

# Format, lint, test, and run the Rust program
make format
make lint
make test
make run

Run the Python version:

make python_install    # install Python deps
make python_test      # run tests
make python_format    # format with Black
make python_lint      # lint with Ruff

Project Structure

Path Purpose
main.py, mylib/ Original Python script and library
src/main.rs, src/lib.rs Rust equivalent (factorial logic)
Makefile Commands for Rust and Python (format, lint, test, run)
Cargo.toml Rust project and dependencies

How the Migration Was Done

  1. Script selection — A small Python script (factorial/data-style processing) was chosen as the migration target.
  2. Rewrite in Rust — The script was reimplemented in Rust with the same behavior.
  3. Testing and debugging — Tests and manual checks ensure Rust output matches Python.
  4. Benchmarking — Execution time and resource usage are compared between Python and Rust.
  5. Documentation — Process and results are documented (including this README).

Makefile Commands

Command Description
make format Format Rust code (cargo fmt)
make lint Lint Rust code (Clippy)
make test Run Rust tests
make run Run the Rust binary
make release Build release binary
make python_install Install Python dependencies
make python_test Run Python tests (pytest)
make python_format Format Python with Black
make python_lint Lint Python with Ruff

Author & Contact

KuchikiRenji


License

See LICENSE in this repository.

About

Migrate Python scripts to Rust: example rewrite with same behavior, tests and performance comparison. Python to Rust migration, Rust vs Python.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors