Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading