In Rust, the concept of Ownership dictates that there cannot be more than one owner of a value at a time. We often want to give access to a value without giving the ownership of it, and to do that we use the concept of borrowing.
Borrowing is creating a reference to a value. A reference is like a Pointer in the sense that it’s a memory address, but Rust guarantees that a reference always points to a valid memory location.
In the following example, the function calculate_length
holds a reference to s1
, and therefore can access its value without taking ownership of it. Because calculate_length
does not own s1
, when the function’s scope ends the value is not dropped, only the reference stored in the parameter s
. This means that we can keep using s1
after the call to calculate_length
, which would not be possible if calculate_length
took ownership of s1
.
In this case, the memory layout would look something like the following:
Like variables, references are also immutable by default. We can create mutable references using the mut
keyword, allowing the value to be modified. This ensures that we explicitly define when a value will be mutated.
An important thing to consider is that when a value has a mutable reference, it can’t have any other references. This property eliminates a class of data race issues that occur when multiple parts of a program can read and write to the same reference. If a value doesn’t have any mutable references, it can have any number of immutable references.
With Lifetimes, Rust guarantees that there are no dangling references. If a value has a reference, the compiler ensures that the value will not go out of scope before the reference does.
References
https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html