I'm writing a parser that stores the Strings comprising the tokens as &'input str
. This has been working well up until I tried to store parsed AST in an environment for future lookup. When I do that, I don't seem to be able to resolve the borrow issues.
Rust Playground is a simplified version of what I'm trying that shows the same issues. It appears that storing the Strings in a Vec to provide a long enough lifetime isn't sufficient. The errors I get are:
<anon>:51:35: 51:39 error: cannot move out of borrowed content [E0507]
<anon>:51 env.push(Box::new(*val));
^~~~
<anon>:44:11: 44:18 error: cannot borrow `history` as mutable because it is also borrowed as immutable [E0502]
<anon>:44 history.push(line);
^~~~~~~
<anon>:46:18: 46:25 note: previous borrow of `history` occurs here; the immutable borrow prevents subsequent moves or mutable borrows of `history` until the borrow ends
<anon>:46 let l = &history.last().unwrap()[..];
^~~~~~~
<anon>:62:4: 62:4 note: previous borrow ends here
<anon>:36 {
...
<anon>:62 }
^
error: aborting due to 2 previous errors
playpen: application terminated with error code 101
Is there a way to workaround what I'm trying to do? Or do I need to move to something different?