Borrowing and Ownership are intimately related concepts that play a vital role in memory management in Rust. Therefore, comprehending borrowing is crucial for working effectively in Rust.
In order to maintain memory safety, Rust uses a borrow checker to enforce its ownership rules. The ownership rules dictate how Rust manages memory over the stack and heap.
Most of the time, we'd like to access some data without taking ownership over it. To accomplish this, Rust provides a borrowing mechanism. Instead of passing objects by-value (T), objects can be passed by reference (&T).
In this Rust tutorial we will learn how to borrow a value temporarily from its owner, which allows us to have multiple references to that value without breaking ownership. We'll also learn about referencing the memory address of a value and how to get the value at that address with the dereferencing operator.
If we want to use a value in a function, without transferring ownership to the function, we can borrow the value temporarily from its owner. When the function is done with the value, it’s given back to the owner.
Borrowing allows us to have one or more references to a single value without breaking the “single owner” concept. When we borrow a value, we reference its memory address with the & operator. A reference in Rust is an address that is passed to a function as an argument.
Output
Inside print_vector function [10, 20, 30]
Printing the value from main() v[0] = 10
A function can modify a borrowed resource by using a mutable reference to such resource. A mutable reference is prefixed with &mut. Mutable references can operate only on mutable variables.
The main() function passes a mutable reference of the variable name to the display() function. The display function appends an additional string to the original name variable.
Output
param_name value is : Hello
The value of name after modification is: Hello World
In Rust, the memory address of a value can be referenced using the & operator. To understand this concept better, let's take a look at an example.
In the example above, the variable a is the owner of the String Hello. If we use the variable in a function, it will pass ownership to the function. In this case we don’t want that, so the solution is to reference the owner (a) instead.
In the parameter list of the function definition, we add the & parameter before the type to tell Rust that the value coming in will be a reference to a value, not the actual value. The function only borrows the value, and gives it back after the function completes its execution.
When we call the function, we have to add the & operator again, this time in front of the value that we pass.
Understanding Ownership and Borrowing is crucial for writing efficient and memory-safe Rust code that compiles and runs without errors.
Failure to adhere to the ownership rules will be detected by the borrow checker, requiring you to modify your code to make it memory-safe before Rust will compile it. The borrow checker is annoying when you’re new to Rust. But, as you write more Rust code, you’ll get used to it and gain experience on writing memory-safe Rust code.
☞ Follow me on Twitter & Linkedin
☞ Kindly subscribe for my upcoming articles