Hi there. I’m somewhat new to Rust but I like it so far. But I frequently stumple over the same problem:
cannot borrow
*self
as mutable more than once at a time
Which is - honestly - a bit annoying. What I want:
Check, if there is an element in an internal Vector. If so, I want to access it, increase the current cursor-position or index and do something with the element. That looks like this:
if self.is_finished() && bundle.allow_next_iteration() {
self.reset(); // Set index to 0
}
if self.has_next() {
return self.dequeue().execute(bundle, self); // dequeue get's the current element and increases the index
}
That - obviously - does not work, since I’m borrowing self as mutable twice. But how can I avoid it? Normally I would just give up and work with D or C++, but Rust is so nice so I want to understand how idiomatic Rust code would look like. If necessary, I can provide a full example, which should be ~150 lines of code.
The four important functions are those:
fn dequeue(&mut self) -> &Box<Middleware<B>>
{
let index = self.index;
self.index += 1;
&self.middlewares[index]
}
fn has_next(&self) -> bool
{
self.middlewares.len() > self.index
}
fn is_finished(&self) -> bool
{
!self.has_next()
}
fn reset(&mut self)
{
self.index = 0;
}