2

I am trying to obtain a reference to the last value of a vector to simplify referencing it in the following code.

fn calculate_errors(&mut self, targets: &[f64]) -> () {r
    let ref mut last_layer = self.vector[self.vector.len() - 1];
    for i in 0..last_layer.len() {
        last_layer[i].error = targets[i] - last_layer[i].value;
    }
}

In my attempt here I get the error:

error[E0502]: cannot borrow self.vector as immutable because it is also borrowed as mutable

What would be the proper way to do this? How could I fix this?

Jonathan Woollett-light
  • 2,813
  • 5
  • 30
  • 58
  • 2
    Hi there! I am pretty sure the linked Q&A answers your question. If that's not the case, please let us know and explain why it is not the case. And hint for the next time asking on SO: make sure you have a [MCVE] (this is neither minimal nor complete) and to include the full error message. Thanks! – Lukas Kalbertodt Aug 12 '19 at 17:46
  • 1
    See also: [Accessing the last element of a Vec or a slice](https://stackoverflow.com/q/28280035/155423); [Get the last element of a vector and push it to the same vector](https://stackoverflow.com/q/37579949/155423); [How to clone last element from vector?](https://stackoverflow.com/q/31907887/155423). – Shepmaster Aug 12 '19 at 17:57

0 Answers0