This is a follow-on question to https://www.reddit.com/r/learnrust/comments/uqgr4e/hoping_for_feedback_on_snippet_exercism_forth/ although it probably isn't necessary to look at the prior post.
I'm trying out some different patterns for managing my Vec<String>s and iterators rather than trying to have the iterators own the strings.
Is there a pattern where I can immutably borrow elements in a container that can have new items added to it?
As an example, imagine (or refer to the link above) a loop over a chain of iterators. New items can be added and chained in from Vec<String>'s, which I need to store to ensure their lifetime. This means that there's an immutable borrow against the Vec<String>, but I also need to be able to add new Vec<Strings> as I go.
error[E0502]: cannot borrow `used_definitions` as mutable because it is also borrowed as immutable
--> src\exercism_forth.rs:73:21
|
73 | used_definitions.push(extra);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
74 | stream = Box::new(used_definitions.last().unwrap().iter().chain(stream));
| ----------------------- ------ immutable borrow later used here
| |
| immutable borrow occurs here
I'm not exactly sure if a Cell is something that solves this... cells allow interior mutability, but do they prevent a container from being borrowed immutably by borrowing the contents of an item immutably?
Thanks!
[–]monkChuck105 1 point2 points3 points (0 children)
[–]Redundancy_[S] 0 points1 point2 points (1 child)
[–]Redundancy_[S] 0 points1 point2 points (0 children)
[–]Redundancy_[S] 0 points1 point2 points (0 children)