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
-
if I replaced the question mark with unwrap .. it works fine .. but I want to return the error to the user
-
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