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.

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).
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.
npm install lua2rust
yarn add lua2rust
pnpm add lua2rust

Demonstrates transpiling a simple Lua function to Rust using CLI.

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