|
1 | | -# Rewriting a Python Script in Rust |
2 | | - |
3 | | -### PURPOSE |
4 | | - |
5 | | -The purpose of this project is to rewrite an existing Python script used for data processing in the Rust programming language. The main goals of this project are as follows: |
6 | | -1. Code Migration: Rewrite the Python script in Rust, ensuring it performs the same data processing tasks. |
7 | | -2. Performance Enhancement: Evaluate the improvements in terms of execution speed and resource usage after the migration to Rust. |
8 | | - |
9 | | -*** |
10 | | - |
11 | | -### PROCESS |
12 | | - |
13 | | -To achieve the objectives of this project, the following steps are taken: |
14 | | -1. Script Selection: Selected an existing Python script for data processing. This script served as a suitable candidate for migration to Rust. |
15 | | -2. Rewrite in Rust: Rewrote the selected Python script in Rust while maintaining the same functionality. This step involved translating the Python code into idiomatic Rust code. |
16 | | -3. Testing and Debugging: Tested the Rust script to ensure it produces the expected results. Debugged and fixed any issues that arose during the migration. |
17 | | -4. Benchmarking: Conducted performance benchmarking tests to evaluate the improvements in execution speed and resource usage achieved by the Rust version of the script. |
18 | | -5. Documentation: Documented the entire process, including the code migration and performance benchmarking. Created a performance comparison report in Markdown format (README). |
19 | | - |
20 | | -*** |
21 | | - |
22 | | -### Commands to Run the Repo |
23 | | - |
24 | | -To run the project, you can use the Makefile and follow these commands: |
25 | | -1. ``` |
26 | | - # To install the required the python packages |
27 | | - make install |
28 | | - ``` |
29 | | -2. ``` |
30 | | - # To check code style |
31 | | - make lint |
32 | | - ``` |
33 | | -3. ``` |
34 | | - # To run tests |
35 | | - make test |
36 | | - ``` |
37 | | -4. ``` |
38 | | - # To format the code |
39 | | - make format |
40 | | - ``` |
41 | | -5. ``` |
42 | | - # To extract data |
43 | | - make extract |
44 | | - ``` |
45 | | -6. ``` |
46 | | - # To tranform data |
47 | | - make transform_load |
48 | | - ``` |
49 | | -7. ``` |
50 | | - # To query data |
51 | | - make query |
52 | | - ``` |
| 1 | +# Rewriting a Python Script in Rust | Python to Rust Migration Example |
53 | 2 |
|
| 3 | +**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. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## What This Project Does |
| 8 | + |
| 9 | +This project **rewrites an existing Python script in Rust** and compares performance. It is a practical example of: |
| 10 | + |
| 11 | +- **Code migration** — Translating a Python data-processing script into idiomatic Rust while keeping behavior identical. |
| 12 | +- **Performance comparison** — Measuring execution speed and resource usage of the Rust version vs the Python version. |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +**Prerequisites:** [Rust](https://www.rust-lang.org/tools/install) (and optionally Python 3 for the original script). |
| 21 | + |
| 22 | +```bash |
| 23 | +# Format, lint, test, and run the Rust program |
| 24 | +make format |
| 25 | +make lint |
| 26 | +make test |
| 27 | +make run |
| 28 | +``` |
| 29 | + |
| 30 | +**Run the Python version:** |
| 31 | + |
| 32 | +```bash |
| 33 | +make python_install # install Python deps |
| 34 | +make python_test # run tests |
| 35 | +make python_format # format with Black |
| 36 | +make python_lint # lint with Ruff |
| 37 | +``` |
| 38 | + |
| 39 | +--- |
| 40 | + |
| 41 | +## Project Structure |
| 42 | + |
| 43 | +| Path | Purpose | |
| 44 | +|------|--------| |
| 45 | +| `main.py`, `mylib/` | Original Python script and library | |
| 46 | +| `src/main.rs`, `src/lib.rs` | Rust equivalent (factorial logic) | |
| 47 | +| `Makefile` | Commands for Rust and Python (format, lint, test, run) | |
| 48 | +| `Cargo.toml` | Rust project and dependencies | |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## How the Migration Was Done |
| 53 | + |
| 54 | +1. **Script selection** — A small Python script (factorial/data-style processing) was chosen as the migration target. |
| 55 | +2. **Rewrite in Rust** — The script was reimplemented in Rust with the same behavior. |
| 56 | +3. **Testing and debugging** — Tests and manual checks ensure Rust output matches Python. |
| 57 | +4. **Benchmarking** — Execution time and resource usage are compared between Python and Rust. |
| 58 | +5. **Documentation** — Process and results are documented (including this README). |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +## Makefile Commands |
| 63 | + |
| 64 | +| Command | Description | |
| 65 | +|--------|-------------| |
| 66 | +| `make format` | Format Rust code (`cargo fmt`) | |
| 67 | +| `make lint` | Lint Rust code (Clippy) | |
| 68 | +| `make test` | Run Rust tests | |
| 69 | +| `make run` | Run the Rust binary | |
| 70 | +| `make release` | Build release binary | |
| 71 | +| `make python_install` | Install Python dependencies | |
| 72 | +| `make python_test` | Run Python tests (pytest) | |
| 73 | +| `make python_format` | Format Python with Black | |
| 74 | +| `make python_lint` | Lint Python with Ruff | |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Author & Contact |
| 79 | + |
| 80 | +**KuchikiRenji** |
| 81 | + |
| 82 | +- **Email:** [KuchikiRenji@outlook.com](mailto:KuchikiRenji@outlook.com) |
| 83 | +- **GitHub:** [github.com/KuchikiRenji](https://github.com/KuchikiRenji) |
| 84 | +- **Discord:** `kuchiki_renji` |
| 85 | + |
| 86 | +--- |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +See [LICENSE](LICENSE) in this repository. |
0 commit comments