Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Struggling with multiple mutable borrows
Can you please teach me what would be the correct, "Rust way" to achieve what I am trying to do?
My expectation is that:
a.find_foo_identifier
find_foo_identifier
returns &str, and I expect this reference to be directly copied from the foo
array, from the borrowed value from inside the Vec
, which I got from iter()
- in fact, I have this map
call in which I do the dereference all the way down to &str
.a
from being borrowed, because the data is copied from it.push_foo
., mutate a
, all cleanpush_bar
, mutate a
again, all good cleanWhat instead happens:
push_foo
requires mutable borrowpush_bar
requires mutable borrowfind_foo_identifier
does an immutable borrow, and it's not compatible with either of the mutable borrows.It's as if everything is just in a permanent borrowed state, shouldn't it be, like, released after the reference is done being used?
I am observing an error at a.push_foo("very good");
. It says: "cannot borrow a
as mutable because it is also borrowed as immutable"
Question: how can I achieve what I'm trying to do in the proper Rust way, so that the borrow checker is satisfied?
Code in question:
struct A<'a> {
foo: Vec<&'a str>,
bar: Vec<&'a str>,
}
impl<'a> A<'a> {
pub fn find_foo_identifier(&self, identifier: &str) -> Option<&str> {
self.foo.iter()
.find(|local_node| **local_node == identifier)
.map(|s|*s)
}
fn push_foo(&mut self, s: &'a str) {
self.foo.push(s);
}
fn push_bar(&mut self, s: &'a str) {
self.bar.push(s);
}
}
fn build_a<'a>() -> A<'a> {
let mut a = A { foo: vec![], bar: vec![] };
for _ in 0..20 {
let found = a.find_foo_identifier("asdf");
if let Some(s) = found {
a.push_foo("very good");
a.push_bar(s);
}
}
a
}
3 posts - 3 participants
🏷️ rust_feed