3 ms·
Rust passing by value/reference: NOTE: The following is my understanding, from discussions on IRC and reading articles. If I'm wrong, please, please, please co
by dorfsmay 8y ago
Rust passing by value/reference:
NOTE: The following is my understanding, from discussions on IRC and reading articles. If I'm wrong, please, please, please correct me (and provide good backup to what you are saying), because this is fundamental, and for me is one of the biggest misunderstanding in Rust.
When deciding to use passing by value vs passing by reference, the only deciding factor should be ownership. If it makes sense to give ownership, pass by value, if it makes sense to retain ownership, pass by reference. This will NOT affect performance, the actual implementation of copying memory around vs reassigning which part of the program owned that piece of memory is an implementation detail and the rust compiler will try to do the right thing.
I stopped fighting the borrow checker once I stopped treating Rust references as if there were C pointers, using them to minimize memory copy instead of concerning myself with ownership. I find the book does a bad job of explaining this, I'd be happy to give a try to write a chapter about it, but need to first confirm that my understanding is correct.