I am implementing a queue, say MyQueue
by wrapping VecDeque
and then implementing certain methods that go directly to the VecDeque
(I have to wrap it because the MyQueue
will ultimately do other things, too.
I have almost everything worked out. I can push, pop, and iterate over the queue.
But, I also need to read the next element without popping it. I need to be able to read non-destructively. A typical peek()
.
When I tried to implement a peek()
or front()
, I ran into a problem, because VecDeque::front()
returns an Option<&Message>
with a reference instead of a value.
When I return a reference, I obviously can't use the MyQueue
object after that. I get
error[E0502]: cannot borrow `queue` as immutable because it is also borrowed as mutable
--> src/data/queue.rs:134:19
|
132 | let peek = queue.peek();
| ----- mutable borrow occurs here
133 | assert_eq!("something3".to_string(), second.kind());
134 | assert_eq!(2, queue.len());
| ^^^^^ immutable borrow occurs here
135 | }
| - mutable borrow ends here
That led me to thinking I should just clone the Message itself and return it, so I'm not returning a reference (borrow) from the MyQueue
.
However, when I try this, I get a cannot move out of borrowed content
.
Here is the relevant code:
impl StdQueue {
fn peek(&mut self) -> Option<Message> {
let r = self.queue.front();
let a = r.unwrap();
let me = *a.clone();
Some(me)
}
}
Which produces:
error[E0507]: cannot move out of borrowed content
--> src/data/queue.rs:68:18
|
68 | let me = *a.clone();
| ^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider using a reference instead: `&*a.clone()`
I'm really not sure where to go from here. How can I create a clone of the Message
inside peek, and then return that value instead of a reference? Or, how can I return a reference, and continue to use the MyQueue
object (I realize this is directly against the entire idea of safe borrowing, which is one thing that brought me to rust in the first place)