Skip to content

Instantly share code, notes, and snippets.

@tjdevries
Created June 15, 2021 14:40
Show Gist options
  • Save tjdevries/54440c0496133ca2b84fdaa8634244f9 to your computer and use it in GitHub Desktop.
Save tjdevries/54440c0496133ca2b84fdaa8634244f9 to your computer and use it in GitHub Desktop.
I DON'T KNOW ANYTHING ABOUT RUST
use mlua::Lua;
use mlua::Result;
fn main() -> Result<()> {
println!("Hello, world!");
let lua = Lua::new();
{
let mut rust_val = Vec::<i32>::new();
lua.scope(|scope| {
// We create a 'sketchy' Lua callback that holds a mutable reference to the variable
// `rust_val`. Outside of a `Lua::scope` call, this would not be allowed
// because it could be unsafe.
lua.globals().set(
"sketchy",
scope.create_function_mut(|_, ()| {
rust_val.insert(0, 1);
Ok(())
})?,
)?;
lua.load("sketchy()").exec()
})?;
assert_eq!(rust_val, vec![1]);
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment