I am writing a program that uses hlua to call Lua code that pushes to a Vec
. The problem is, I have multiple Lua methods that push to the same Vec
. Here is a very simplified version of what I am trying to do:
let mut vec = Vec::new();
{
let mut lua = Lua::new();
// these functions do different things in my actual code
lua.set("pushA", hlua::function1(|s: String| {
vec.push(s);
}));
lua.set("pushB", hlua::function1(|s: String| {
vec.push(s);
}));
lua.execute::<()>("...");
}
The compiler errors with this:
error[E0499]: cannot borrow `vec` as mutable more than once at a time
--> src/main.rs:11:42
|
10 | lua.set("pushA", hlua::function1(|s: String| { vec.push(s); }));
| ----------- --- previous borrow occurs due to use of `vec` in closure
| |
| first mutable borrow occurs here
11 | lua.set("pushB", hlua::function1(|s: String| { vec.push(s); }));
| ^^^^^^^^^^^ --- borrow occurs due to use of `vec` in closure
| |
| second mutable borrow occurs here
...
14 | }
| - first borrow ends here
which I understand, but I can't come up with a solution to my problem where this would not occur in one way or another. I am very new with Rust, so this may be a dumb question.