Created
February 10, 2026 15:31
-
-
Save vnphanquang/2861136619bb12d4f9318b4223e52109 to your computer and use it in GitHub Desktop.
Call Lua From Rust
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [package] | |
| name = "call-lua-from-rust" | |
| version = "0.1.0" | |
| edition = "2024" | |
| [dependencies] | |
| mlua = { version = "0.11", features = ["lua54", "vendored"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| LUA_VARIABLE = "Hello from Lua!" | |
| --- @param name string | |
| --- @return string | |
| function LUA_FUNCTION(name) | |
| return "Hello, " .. name .. "!" | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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