所要時間: 5分
hello.py を作成:
def greet(name: str) -> str:
return f"Hello, {name}!"
def main():
message: str = greet("Tsuchinoko")
print(message)
main()
トランスパイルして実行:
tnk hello.py -o hello.rs
rustc hello.rs -o hello
./hello
出力:
Hello, Tsuchinoko!
git clone https://github.com/tanep3/Tsuchinoko.git
cd Tsuchinoko
cargo build --release
cargo install --path .
インストール後、tnk コマンドがグローバルで使用可能になります。
tnk your_file.py
出力先: カレントディレクトリの your_file.rs
tnk your_file.py -o custom_output.rs
外部ライブラリ (NumPy, Pandas など) を使う場合:
# まず venv を有効化
source venv/bin/activate
tnk your_file.py --project my_project
cd my_project
cargo run --release
以下の構造のCargoプロジェクトが生成されます:
my_project/
├── Cargo.toml
├── .gitignore
└── src/
└── main.rs
tnk your_file.py --check
出力を生成せずにPythonコードを検証します。
| オプション | 短縮形 | 説明 |
|---|---|---|
--output |
-o |
出力ファイルパスを指定 |
--project |
-p |
Cargoプロジェクトを生成 |
--check |
-c |
チェックのみ(出力なし) |
--debug |
-d |
デバッグ情報を表示 |
--help |
-h |
ヘルプを表示 |
--version |
-V |
バージョンを表示 |
すべての変数と関数シグネチャに型ヒントが必須です:
# ✅ OK
x: int = 10
def add(a: int, b: int) -> int:
return a + b
# ❌ NG(型ヒントなし)
x = 10
def add(a, b):
return a + b
標準的なPythonエントリポイントパターンを使用してください:
def main() -> None:
# your code here
pass
if __name__ == "__main__":
main()
これにより適切なRustの main() 関数が生成されます。
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 パターン
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型 | Rust型 |
|---|---|
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 |
() |
リストパラメータは自動的に参照渡しになります:
def process(data: list[int]) -> int: # dataは &[i64] になる
return len(data)
**kwargs (キーワード可変長引数)yieldasync/awaitraise ... from ... (v1.5.1 で対応予定)try/except/else (v1.5.1 で対応予定)max() はpanicする(Pythonと同様)examples/ ディレクトリに動作するサンプルがあります:
examples/simple/ - 基本的な変換サンプル (54ファイル)examples/import/ - 外部ライブラリサンプル (8ファイル)examples/benchmarks/ - パフォーマンスベンチマーク