### Run async examples via Cargo Source: https://docs.rs/crate/mlua/latest Commands to execute various async examples with specific feature flags enabled. ```shell # async http client (hyper) cargo run --example async_http_client --features=lua54,async,macros # async http client (reqwest) cargo run --example async_http_reqwest --features=lua54,async,macros,serde # async http server cargo run --example async_http_server --features=lua54,async,macros,send curl -v http://localhost:3000 ``` -------------------------------- ### Build with custom Lua library Source: https://docs.rs/crate/mlua/latest Example of using environment variables to link a custom Lua library during compilation. ```shell my_project $ LUA_LIB=$HOME/tmp/lua-5.2.4/src LUA_LIB_NAME=lua LUA_LINK=static cargo build ``` -------------------------------- ### Configure Custom Lua Library Source: https://docs.rs/crate/mlua/latest/source/README.md Example of setting environment variables to link a custom Lua library during compilation. ```sh LUA_LIB=$HOME/tmp/lua-5.2.4/src LUA_LIB_NAME=lua LUA_LINK=static cargo build ``` -------------------------------- ### Basic Lua runtime usage Source: https://docs.rs/crate/mlua/latest Demonstrates initializing a Lua state, manipulating tables, and executing Lua code from Rust. ```rust use mlua::prelude::*; fn main() -> LuaResult<()> { let lua = Lua::new(); let map_table = lua.create_table()?; map_table.set(1, "one")?; map_table.set("two", 2)?; lua.globals().set("map_table", map_table)?; lua.load("for k,v in pairs(map_table) do print(k,v) end").exec()?; Ok(()) } ``` -------------------------------- ### Rust Panic Handling in Lua Source: https://docs.rs/crate/mlua/latest Demonstrates how Rust panics are caught and propagated as Lua errors using pcall. ```rust let lua = Lua::new(); let f = lua.create_function(|_, ()| -> LuaResult<()> { panic!("test panic"); })?; lua.globals().set("rust_func", f)?; let _ = lua.load(r#" local status, err = pcall(rust_func) print(err) -- prints: test panic error(err) -- propagate panic "#).exec(); unreachable!() ``` -------------------------------- ### Build and Run Lua Module on macOS Source: https://docs.rs/crate/mlua/latest Compile the Rust code with specific linker arguments and load the module in Lua. ```bash $ cargo rustc -- -C link-arg=-undefined -C link-arg=dynamic_lookup $ ln -s ./target/debug/libmy_module.dylib ./my_module.so $ lua5.4 -e 'require("my_module").hello("world")' ``` -------------------------------- ### Rust Code for Lua Module Source: https://docs.rs/crate/mlua/latest Defines a 'hello' function and exposes it via the `#[mlua::lua_module]` macro. ```rust use mlua::prelude::*; fn hello(_: &Lua, name: String) -> LuaResult<()> { println!("hello, {}!", name); Ok(()) } #[mlua::lua_module] fn my_module(lua: &Lua) -> LuaResult { let exports = lua.create_table()?; exports.set("hello", lua.create_function(hello)?) Ok(exports) } ``` -------------------------------- ### Handle Rust Panics in Lua Source: https://docs.rs/crate/mlua/latest/source/README.md Demonstrates how Rust panics are caught by mlua and converted into Lua errors. These errors can then be handled using Lua's `pcall` or propagated back to Rust. ```rust let lua = Lua::new(); let f = lua.create_function(|_, ()| -> LuaResult<()> { panic!("test panic"); })?; lua.globals().set("rust_func", f)?; let _ = lua.load(r#" local status, err = pcall(rust_func) print(err) -- prints: test panic error(err) -- propagate panic "#).exec(); unreachable!() ``` -------------------------------- ### Configure Cargo.toml for Lua Module Source: https://docs.rs/crate/mlua/latest Specify the crate type as cdylib and add mlua with 'lua54' and 'module' features. ```toml [lib] crate-type = ["cdylib"] [dependencies] mlua = { version = "0.11", features = ["lua54", "module"] } ``` -------------------------------- ### Compile and Run Lua Module on macOS Source: https://docs.rs/crate/mlua/latest/source/README.md Commands to compile a Rust library as a dynamic lookup object and then load and execute it using Lua 5.4. ```sh cargo rustc -- -C link-arg=-undefined -C link-arg=dynamic_lookup ln -s ./target/debug/libmy_module.dylib ./my_module.so lua5.4 -e 'require("my_module").hello("world")' ``` -------------------------------- ### macOS Cargo Configuration for Dynamic Lookup Source: https://docs.rs/crate/mlua/latest Configure Rust flags in .cargo/config.toml for macOS targets to enable dynamic lookup. ```toml [target.x86_64-apple-darwin] rustflags = [ "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup", ] [target.aarch64-apple-darwin] rustflags = [ "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup", ] ``` -------------------------------- ### Configure Rust flags for Lua symbol export Source: https://docs.rs/crate/mlua/latest/source/FAQ.md Add these settings to .cargo/config to ensure Lua symbols are properly exported when loading C modules on Linux or macOS. ```toml [target.x86_64-unknown-linux-gnu] rustflags = ["-C", "link-args=-rdynamic"] [target.x86_64-apple-darwin] rustflags = ["-C", "link-args=-rdynamic"] ``` -------------------------------- ### Configure Rust Flags for macOS Dynamic Lookup Source: https://docs.rs/crate/mlua/latest/source/README.md This TOML configuration sets Rust linker arguments for macOS targets to enable dynamic lookup, necessary for building Lua modules. ```toml [target.x86_64-apple-darwin] rustflags = [ "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup", ] [target.aarch64-apple-darwin] rustflags = [ "-C", "link-arg=-undefined", "-C", "link-arg=dynamic_lookup", ] ``` -------------------------------- ### Add mlua to Cargo.toml for Module Mode Source: https://docs.rs/crate/mlua/latest/source/README.md Configure your Cargo.toml for creating a dynamic library (cdylib) that can be loaded as a Lua module, using Lua 5.4. ```toml [lib] crate-type = ["cdylib"] [dependencies] mlua = { version = "0.11", features = ["lua54", "module"] } ``` -------------------------------- ### Typos Configuration Source: https://docs.rs/crate/mlua/latest/source/typos.toml Configuration file for the typos tool, defining ignored identifiers, regex patterns, and custom word overrides. ```toml [default] extend-ignore-identifiers-re = ["2nd", "ser"] extend-ignore-re = [ # Custom ignore regex patterns: https://github.com/crate-ci/typos/blob/master/docs/reference.md#defaultextend-ignore-re ".*(?:#|--|//|/*).*(?:spellchecker|typos):\\s?ignore[^\\n]*\\n", ".*(?:spellchecker|typos):\\s?ignore-next-line[^\\n]*\\n[^\\n]*", ] [files] extend-exclude = ["tests/compile/*.stderr"] [default.extend-words] thr = "thr" aas = "aas" ``` -------------------------------- ### Add mlua dependency Source: https://docs.rs/crate/mlua/latest Configuration for Cargo.toml to include mlua with Lua 5.4 and vendored support. ```toml [dependencies] mlua = { version = "0.11", features = ["lua54", "vendored"] } ``` -------------------------------- ### Define a Lua Module in Rust Source: https://docs.rs/crate/mlua/latest/source/README.md This Rust code defines a Lua module named 'my_module' with an exported 'hello' function. The function takes a string argument and prints a greeting. ```rust use mlua::prelude::*; fn hello(_: &Lua, name: String) -> LuaResult<()> { println!("hello, {}!", name); Ok(()) } #[mlua::lua_module] fn my_module(lua: &Lua) -> LuaResult { let exports = lua.create_table()?; exports.set("hello", lua.create_function(hello)?) Ok(exports) } ``` -------------------------------- ### Generic Numeric Sequence Source: https://docs.rs/crate/mlua/latest/source/typos.toml A simple numeric sequence used for testing or placeholder purposes. ```text 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.