Borrow checker with mut self and ? operator

Hello

I am newbie to Rust and I need help
I was writing struct representing mathematical matrix ... the matrix uses Vec in internal data structure so I have a method like this to map row column to index with immutable self

    pub fn get_index(&self, x:usize, y:usize) -> Result<usize, &str> {
        if x>0 && x<= self.width && y>0 && y<=self.height
            { Ok( (y-1) * self.width + (x-1) ) }
        else
            { Err("out of boundries ")}
    }

and I use it in a function to set value for the element defined by row, column

    pub fn set_elem(&mut self, x:usize, y:usize, val:f32) -> Result<(),&str>  {
        let idx = self.get_index(x, y)?;
        self.elements[idx] = val;
        Ok(())
    }

here are some points related to it

  1. if I replaced the question mark with unwrap .. it works fine .. but I want to return the error to the user

  2. the error I get is like that
    error[E0502]: cannot borrow *self as mutable because it is also borrowed as immutable
    --> matrix.rs:87:13
    |
    84 | pub fn set_elem(&mut self, x:usize, y:usize, val:f32) -> Result<(),&str> {
    | - let's call the lifetime of this reference '1
    85 |
    86 | let idx = self.get_index(x, y)?;
    | ---- - returning this value requires that *self is borrowed for '1
    | |
    | immutable borrow occurs here
    87 | self.elements[idx] = val;
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here

I could not understand the semantics here and how to solve

This actual means

pub fn get_index<'a>(&'a self, x:usize, y:usize) -> Result<usize, &'a str> {

which basically says that the &str is stored in self.

What you want is

pub fn get_index(&self, x:usize, y:usize) -> Result<usize, &'static str> {

You are only allowed to elided a lifetime in the function return type if that lifetime is the same as a function argument. If your function didn't have an argument with a lifetime you would have gotten an error.

You can read the reference's page on lifetime elision for more details.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.