Borrow immutability

If we look back to the start of this chapter, I described borrowing as borrowing a DVD from my son. When I take possession of the DVD, I cannot change it, as my son would expect the same DVD back.

The same applies for Rust: the references cannot be changed as they are immutable values. If you think about it, this makes sense. Let me explain.

I have created a Vec<T> array type, which is, let's say, eight values long (it doesn't matter what the values are or the type they are). When the binding between the heap and stack is made, it will be of a particular type. If we allow the reference to alter the vector, we will have the same problem as the C# example and so the guarantees can't be assured and the Rust compiler will fail the build. To ensure that the guarantee is kept, Rust simply says that you are unable to change the values borrowed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset