Time Required: 5 minutes
Create hello.py:
def greet(name: str) -> str:
return f"Hello, {name}!"
def main():
message: str = greet("Tsuchinoko")
print(message)
main()
Transpile and run:
tnk hello.py -o hello.rs
rustc hello.rs -o hello
./hello
Output:
Hello, Tsuchinoko!
git clone https://github.com/tanep3/Tsuchinoko.git
cd Tsuchinoko
cargo build --release
cargo install --path .
After installation, tnk command will be available globally.
tnk your_file.py
Output: your_file.rs in current directory
tnk your_file.py -o custom_output.rs
For scripts using external libraries (NumPy, Pandas, etc.):
# Activate venv first
source venv/bin/activate
tnk your_file.py --project my_project
cd my_project
cargo run --release
This creates a complete Cargo project structure:
my_project/
├── Cargo.toml
├── .gitignore
└── src/
└── main.rs
tnk your_file.py --check
Validates the Python code without generating output.
| Option | Short | Description |
|---|---|---|
--output |
-o |
Specify output file path |
--project |
-p |
Generate Cargo project |
--check |
-c |
Check only, no output |
--debug |
-d |
Show debug information |
--help |
-h |
Show help message |
--version |
-V |
Show version |
All variables and function signatures must have type hints:
# ✅ Good
x: int = 10
def add(a: int, b: int) -> int:
return a + b
# ❌ Bad (no type hints)
x = 10
def add(a, b):
return a + b
Use the standard Python entry point pattern:
def main() -> None:
# your code here
pass
if __name__ == "__main__":
main()
This will generate a proper Rust main() function.
nums: list[int] = [1, 2, 3, 4, 5]
doubled: list[int] = [x * 2 for x in nums]
nums.append(6)
first: int = nums.pop(0)
scores: dict[str, int] = {"Alice": 90, "Bob": 85}
alice_score: int = scores["Alice"]
scores["Charlie"] = 88
for key in scores.keys():
print(key)
s: set[int] = {1, 2, 3}
s.add(4)
s.remove(1)
union: set[int] = s | {5, 6}
from typing import Optional
def find(items: list[int], target: int) -> Optional[int]:
for i, item in enumerate(items):
if item == target:
return i
return None
result: Optional[int] = find([1, 2, 3], 2)
value: int = result or -1 # x or default pattern
nums: list[int] = [0, 1, 2, 3, 4, 5]
first_three: list[int] = nums[:3]
reversed_nums: list[int] = nums[::-1]
every_other: list[int] = nums[::2]
try:
result: int = int("abc")
except ValueError as e:
print("Invalid input")
finally:
print("Cleanup")
| Python Type | Rust Type |
|---|---|
int |
i64 |
float |
f64 |
str |
String |
bool |
bool |
list[T] |
Vec<T> |
dict[K, V] |
HashMap<K, V> |
set[T] |
HashSet<T> |
tuple[T, U] |
(T, U) |
Optional[T] |
Option<T> |
None |
() |
List parameters are automatically passed by reference:
def process(data: list[int]) -> int: # data becomes &[i64]
return len(data)
**kwargs (keyword arguments)yieldasync/awaitraise ... from ... (planned for v1.5.1)try/except/else (planned for v1.5.1)max() will panic (same as Python)See the examples/ directory for working examples:
examples/simple/ - Basic transpilation examples (54 files)examples/import/ - External library examples (8 files)examples/benchmarks/ - Performance benchmarks