Created
June 15, 2021 14:40
-
-
Save tjdevries/54440c0496133ca2b84fdaa8634244f9 to your computer and use it in GitHub Desktop.
I DON'T KNOW ANYTHING ABOUT 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
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