Python to Rust Transpiler - Convert type-hinted Python code to Rust
Tsuchinoko is a transpiler that converts type-hinted Python code to Rust.
Write algorithmic logic in Python’s readable syntax and gain Rust’s safety and performance.
Tsuchinoko (ツチノコ) is a legendary snake-like cryptid from Japanese folklore. Like its namesake, this tool transforms one thing (Python) into something unexpected (Rust!).
Coverage: Supports 57% of Python syntax features (86 of 151 features), covering essential constructs for algorithmic programming: variables, operators, control flow, functions, classes (with inheritance), @property, data structures, collections (list/dict/set), string methods, resource management (with statement), and robust error handling with Result types.
Tsuchinoko is not a general-purpose Python compiler. It is designed to:
&[T], &str) where possible to avoid unnecessary allocations.lambda x: x + 1 → |x| x + 1__init__ and methods[x*2 for x in nums if x > 0]{1, 2, 3} → HashSetx or default, None check with ternaryarr[::2], arr[::-1]mutnumpy / pandas via persistent IPC workerDynamicValue enum + matchHashMap<String, Value>Comparison between Python (3.x) and Tsuchinoko-generated Rust code (compiled with rustc -O).
Benchmarks include data generation (LCG) and sorting time.
| Algorithm | N | Python | Tsuchinoko (Rust) | Speedup |
|---|---|---|---|---|
| Bubble Sort | 10,000 | 5.394s | 0.037s | ~146x 🚀 |
| Radix Sort | 10,000,000 | 8.908s | 0.278s | ~32x 🚀 |
Measured using hyperfine on local environment (Linux x86_64, V1.0.0).
git clone https://github.com/tanep3/Tsuchinoko.git
cd Tsuchinoko
cargo build --release
cargo install --path .
# Basic transpilation
tnk your_file.py
# Specify output
tnk your_file.py -o output.rs
# Generate Cargo project
tnk your_file.py --project my_project
# Check only (no output)
tnk your_file.py --check
[!NOTE] If your code uses
importstatements (Resident Worker), use--projectto generate a valid Cargo project with dependencies.
[!IMPORTANT] venv required: When using the Resident Worker (NumPy/Pandas etc.), run
tnkwithin an activated Python virtual environment, and execute the generated binary in the same venv.source venv/bin/activate tnk script.py --project my_app cd my_app && cargo run --release
def bubble_sort(lists: list[int]) -> tuple[list[int], int]:
sorted_list: list[int] = list(lists)
list_length: int = len(sorted_list)
for i in range(list_length):
for j in range(list_length - i - 1):
if sorted_list[j] > sorted_list[j + 1]:
temp: int = sorted_list[j]
sorted_list[j] = sorted_list[j + 1]
sorted_list[j + 1] = temp
return sorted_list, list_length
fn bubble_sort(lists: &[i64]) -> (Vec<i64>, i64) {
let mut sorted_list: Vec<i64> = lists.to_vec();
let list_length: i64 = sorted_list.len() as i64;
for i in 0..list_length {
for j in 0..((list_length - i) - 1) {
if sorted_list[j as usize] > sorted_list[(j + 1) as usize] {
let temp: i64 = sorted_list[j as usize];
sorted_list[j as usize] = sorted_list[(j + 1) as usize];
sorted_list[(j + 1) as usize] = temp;
}
}
}
return (sorted_list, list_length);
}
Transform Python to Rust directly in your editor!
Ctrl+Alt+P) - See transpiled code in real-time| 📖 Setup Guide | セットアップガイド |
For detailed feature lists, see:
MIT License
Tane Channel Technology