Mutating VecDeque inside struct

I am trying to mutate VecDeque inside struct. I want to receive an event and delete it from Vec::Deque.

pub struct Timeline {
    pub event_loop: Option<VecDeque<Events>>
}

impl Timeline {

    pub fn get_event(&mut self)-> Option<Events> {
        if let Some(mut sequence) = self.event_loop{
           sequence.pop_front()
        } else{
            None
        }
    }

got this error

  --> src/timeline.rs:33:37
   |
33 |         if let Some(mut sequence) = self.event_loop{
   |                     ------------    ^^^^^^^^^^^^^^^ help: consider borrowing here: `&self.event_loop`
   |                     |
   |                     data moved here
   |                     move occurs because `sequence` has type `VecDeque<Events>`, which does not implement the `Copy` trait`

I can fix this error by taking ownership of entire object get_event(&mut self) change to get_event(self), but this is not an option because after getting 1 event from get_event(), the whole object is gone and I am receiving "value used after move"

>Solution :

Add &mut and remove the mut:

if let Some(sequence) = &mut self.event_loop {

Or replace the mut with ref mut:

if let Some(ref mut sequence) = self.event_loop {

Leave a Reply