Skip to content

Instantly share code, notes, and snippets.

@vnphanquang
Created February 10, 2026 15:31
Show Gist options
  • Select an option

  • Save vnphanquang/2861136619bb12d4f9318b4223e52109 to your computer and use it in GitHub Desktop.

Select an option

Save vnphanquang/2861136619bb12d4f9318b4223e52109 to your computer and use it in GitHub Desktop.
Call Lua From Rust
[package]
name = "call-lua-from-rust"
version = "0.1.0"
edition = "2024"
[dependencies]
mlua = { version = "0.11", features = ["lua54", "vendored"] }
LUA_VARIABLE = "Hello from Lua!"
--- @param name string
--- @return string
function LUA_FUNCTION(name)
return "Hello, " .. name .. "!"
end
// https://github.com/mlua-rs/mlua
// https://github.com/mlua-rs/mlua/blob/main/examples/guided_tour.rs
use mlua::prelude::*;
use mlua::{Function};
use std::path::Path;
fn main() -> LuaResult<()> {
let lua = Lua::new();
let file_path = Path::new("src/config.lua");
lua.load(file_path).exec()?;
let globals = lua.globals();
let script_variable: String = globals.get("LUA_VARIABLE")?;
println!("Value from Lua script: {}", script_variable);
let script_function: Function = globals.get("LUA_FUNCTION")?;
let returned = script_function.call::<String>("from Rust")?;
println!("Returned from Lua function: {}", returned);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment