lua2rust
raw JSON → 0.5.2 verified Fri May 01 auth: no javascript
A transpiler that converts Lua source code to Rust. Current stable version 0.5.2. Release cadence is irregular with occasional updates. Key differentiator: it provides a direct path from Lua scripts to Rust code, though with significant limitations such as no support for metatables, loadstring, or user-defined iterators. It is best suited for simple Lua scripts that use basic constructs.
Common errors
error error: Found argument 'script.lua' which wasn't expected, or isn't valid in this context ↓
cause Incorrect CLI usage – missing or extra arguments.
fix
Use: npx lua2rust <lua file> <output function name>
error thread 'main' panicked at 'index out of bounds: ...' ↓
cause String index out of range in transpiled Rust code.
fix
Ensure Lua string indices are within bounds (1-based).
Warnings
gotcha Transpiled code uses Rust's `.chars()` for string indexing, which can panic on invalid indices. ↓
fix Ensure Lua string indices are valid; consider wrapping in Rust's try/catch.
gotcha Table operations like `table.insert` and `#t` only work on consecutive arrays starting at index 1. ↓
fix Restructure Lua tables to be 1-indexed consecutive arrays.
gotcha Modifying a table while iterating over it leads to undefined behavior. ↓
fix Avoid modifying tables during iteration; collect changes and apply afterward.
gotcha `tonumber` with base argument is not implemented. ↓
fix Manually implement base conversion in Lua before transpiling.
Install
npm install lua2rust yarn add lua2rust pnpm add lua2rust Imports
- default (transpiled function) wrong
import { transpile } from 'lua2rust'correct// no import; transpiled output is a Rust function - cli invocation wrong
lua2rust script.luacorrectnpx lua2rust script.lua my_function > output.rs - CommonJS require (not applicable) wrong
const lua2rust = require('lua2rust')correct
Quickstart
// Create a simple Lua script
// test.lua:
function greet(name)
return "Hello, " .. name
end
print(greet("World"))
# Transpile to Rust
npx lua2rust test.lua greet > greet.rs
// greet.rs will contain a Rust function corresponding to the Lua code.