Error message confused me


#1

I missed out the & in d_use and I got an error message telling me:

 --> src/main.rs:6:9
  |
6 |         self.d_use()
  |         ^^^^ cannot move out of borrowed content
struct Foo {
    v:Vec<usize>
}
impl Foo {
    fn ind_use(&self) -> usize{
        self.d_use()
    }
    fn d_use(self) -> usize {
        *self.v.first().unwrap()
    }
}
fn main() {
}

Is this a case where a more direct message about a missing & would be more appropriate or am I missing something?


#2

imo that would be a bad idea for 2 reasons

  1. in many cases you can make self copy and then you don’t need the &
  2. how should the error look if the function is declared in an external crate? you can’t add the & there.

#3

I had a similar experience. I am learning Rust, and I was following the exercises in the Rust online book chapter 8.3. The code was not working, and I was following the compiler error messages blindly - doing whatever it was suggesting, and it was leading me to nowhere.

I referred O’Reilly Programming Rust - and learned the ownership and lifetime concepts. I was able to handle the problems without much difficulty. And of course, if you are using the APIs better read its documentation, see how the API is borrowing the data - & or &mut.

Conclusion:

Learn the concepts. I have found the compiler messages useful sometimes, they usually point out the basic problem, and you have to know the concept to understand and fix the problem.