Transplang
Translate a strict subset of Python scripts into compilable Rust programs.
Example
script.py
def total(nums):
s = 0
for n in nums:
s += n
return s
print(total([1, 2, 3, 4]))
Generated out.rs
fn total(nums: Vec<i32>) -> i32 {
let mut s: i32 = 0;
for n in nums {
s = s + n;
}
s
}
fn main() {
println!("{}", total(vec![1, 2, 3, 4]));
}
Supported Python constructs
| Construct | Notes |
|---|---|
def functions |
scalar and Vec parameters |
| Integer literals | mapped to i32 |
| String literals | mapped to &str |
| List literals | mapped to vec![…] |
| Variable assignment | let / let mut |
| Augmented assignment | +=, -=, *=, /= |
| Arithmetic | +, -, *, / |
for x in y loops |
iterating over lists or Vec params |
return |
last return rendered as tail expression |
print() |
mapped to println! |
Unsupported (rejected with a clear error)
importstatementsclassdefinitionsif/while/with/tryasync/await- Decorators
- Comprehensions
- Dicts, sets
- Attribute access (
obj.attr) - Subscript access (
x[i])
Architecture
Python file
↓
ast.parse() [parser.py]
↓
SemanticModel [analyzer.py]
↓
RustIR [mapper.py]
↓
Rust source [rust_generator.py]
No runtime dependencies; stdlib only (ast, argparse, pathlib).
Install
pip install transplang
Or from source:
git clone https://github.com/calchiwo/transplang.git
cd transplang
pip install -e .
Usage
transplang translate script.py --to rust
Write output to a file:
transplang translate script.py --to rust --output out.rs
rustc out.rs && ./out
Running tests
pip install pytest
pytest
Or without pytest:
python -m unittest discover -s tests
Scope
Transplang currently focuses on a very small subset of Python and Rust.
The goal of this version is simple: demonstrate that structured translation between programming languages can work in practice when the surface area is tightly controlled.
Only a limited set of constructs are supported:
- functions
- integers
- lists
- loops
- basic arithmetic
print
Anything outside this scope is intentionally rejected.
This constraint keeps the system deterministic and makes it easier to reason about translation correctness.
Why I started small
Real software systems are complex. Languages differ in runtime models, type systems, and ecosystem conventions.
Attempting to translate entire applications immediately would produce unreliable results.
Instead, Transplang begins with a narrow slice of functionality to test the core idea: that a program can be analyzed structurally and rebuilt in another language.
If this approach proves reliable on small programs, the surface area can expand over time.
Long-term direction
Programs written in different languages ultimately describe the same underlying elements:
- state
- transformations
- control flow
- side effects
Transplang explores whether those elements can be extracted and reconstructed in another language environment.
The current prototype should be viewed as a minimal experiment around that idea.
Author
Metadata
Release files for transplang 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| transplang-0.1.0.tar.gz | 12.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| transplang-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.4 kB
Release files / transplang-0.1.0.tar.gz
| Download URL | transplang-0.1.0.tar.gz |
|---|---|
| Size | 12.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2596b1eb8461d08110dca19e1cf9a2ccceff595ffded24c1794694b0f711ba54
|
|
BLAKE2b-256 checksum How to use checksums |
54d0d9cc774a8c092a958aacc8c3eed3d3d749331feef3233ad590866a5b67be
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|
Release files / transplang-0.1.0-py3-none-any.whl
| Download URL | transplang-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
712dc79c8875e897903745df838402bb2862fcfe8d5af79ace4217e79d105bca
|
|
BLAKE2b-256 checksum How to use checksums |
d2a93c56fa0be10a65eb52ab8ad95f833dda78a393e386ee1bf3113e74ceaa4a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.2
|