Having trouble using Struct Option var

Compiling the following code:

pub struct Chessboard {
    pub clip_buffer: Option<RenderBuffer>,
}
iimpl Chessboard {
    pub fn new() -> Chessboard {
        Chessboard {
            clip_buffer: None,
        }
    }
    pub fn get_clip_from_buffer(&mut self) {
        ...
        ...
        match self.clip_buffer {
                    Some(buffer) => buffer.set_pixel(clipx, clipy, color),
                    _ => {},
                }
    }
}

gives me these two errors:

error[E0507]: cannot move out of `self.clip_buffer.0` which is behind a mutable reference
   --> src/chessboard.rs:186:23
    |
186 |                 match self.clip_buffer {
    |                       ^^^^^^^^^^^^^^^^ help: consider borrowing here: `&self.clip_buffer`
187 |                     Some(buffer) => buffer.set_pixel(clipx, clipy, color),
    |                          ------
    |                          |
    |                          data moved here
    |                          move occurs because `buffer` has type `graphics_buffer::RenderBuffer`, which does not implement the `Copy` trait

error[E0596]: cannot borrow `buffer` as mutable, as it is not declared as mutable
   --> src/chessboard.rs:187:37
    |
187 |                     Some(buffer) => buffer.set_pixel(clipx, clipy, color),
    |                          ------     ^^^^^^ cannot borrow as mutable
    |                          |
    |                          help: consider changing this to be mutable: `mut buffer`

when I follow the error suggestions compiling the following code:

pub struct Chessboard {
    pub clip_buffer: Option<RenderBuffer>,
}
iimpl Chessboard {
    pub fn new() -> Chessboard {
        Chessboard {
            clip_buffer: None,
        }
    }
    pub fn get_clip_from_buffer(&mut self) {
        ...
        ...
        match &self.clip_buffer {
                    Some(mut buffer) => buffer.set_pixel(clipx, clipy, color),
                    _ => {},
                }
    }
}

I get this error:

error[E0507]: cannot move out of a shared reference
   --> src/chessboard.rs:186:23
    |
186 |                 match &self.clip_buffer {
    |                       ^^^^^^^^^^^^^^^^^
187 |                     Some(mut buffer) => buffer.set_pixel(clipx, clipy, color),
    |                          ----------
    |                          |
    |                          data moved here
    |                          move occurs because `buffer` has type `graphics_buffer::RenderBuffer`, which does not implement the `Copy` trait

RenderBuffer comes from an extern crate.
How can I successfully use the self.clip_buffer car?

Try self.clip_buffer.as_mut()

Thank you!

Alternatives:

match &mut self.clip_buffer {
    Some(buffer) => buffer.set_pixel(clipx, clipy, color),
    None => {},
}
match self.clip_buffer {
    Some(ref mut buffer) => buffer.set_pixel(clipx, clipy, color),
    None => {},
}

I've filed an issue about the erroneous suggestion in the error message: #74617

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.