Tsuchinoko

Tsuchinoko 🐍➡️🦀

Python to Rust Transpiler - Convert type-hinted Python code to Rust

Rust License Version Coverage Changelog

🇯🇵 日本語版はこちら

Overview

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.

Design Philosophy

Tsuchinoko is not a general-purpose Python compiler. It is designed to:

Key Features ✨

Benchmarks 🚀

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).

Installation

git clone https://github.com/tanep3/Tsuchinoko.git
cd Tsuchinoko
cargo build --release
cargo install --path .

Usage

# 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 import statements (Resident Worker), use --project to generate a valid Cargo project with dependencies.

[!IMPORTANT] venv required: When using the Resident Worker (NumPy/Pandas etc.), run tnk within 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

Input Example (Python)

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

Output Example (Rust)

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);
}

VS Code Extension

Transform Python to Rust directly in your editor!

📖 Setup Guide セットアップガイド

Feature Documentation

For detailed feature lists, see:

Future Roadmap

Documentation

License

MIT License

Author

Tane Channel Technology