Ownership And Borrowing In Rust
Tags: rust
Published: 2022-05-11 11:15:02

<!-- Yay, no errors, warnings, or alerts! -->

In this article, you will learn how ownership and borrowing works, and what they are in Rust.

<h3>Prerequisites</h3>


To follow through this article, you need to have at least a basic knowledge of Rust, and you need the Rust compiler installed in your system.

<h3>What is ownership</h3>


Ownership is a set of rules that regulates memory usage in rust programs. These rules are key to rust’s memory safety, despite the lack of a garbage collector. Ownership provides a way for the Rust compiler to track all allocated memory at compile time.

The following are the rules governing ownership:



1. Each value must have only one owner at any time: that means that if you attempt to store an owner in another one the previous one passes its value to the new owner and loses its value.
2. An owner can only exist in one scope: that means if you pass an owner to a function, the owner leaves its current scope and moves to the new function’s scope. The example below shows `name` being moved from `main`’s scope to `greet`’s scope: 
```Rust
fn main() {
    let name = "John".to_string();
    greet(name);
    println!("Name: {}", name);     // <- throws an error because it is out of scope
}

fn greet(name: String) {
    println!("Hello, {}", name);
}
```



Owners in Rust are similar to variables or constants in other programming languages.

<h3>Borrowing and References</h3>


Borrowing allows functions to reference owners from a scope without moving them to the new function’s scope. To borrow an owner, you first need to get a reference to it. To get the reference, you need the & operator:


```Rust
fn main() {
    let name = "John".to_string();
    greet(&name);
    println!("Name: {}", name);
}

fn greet(name: &String) {
    println!("Hello, {}", name);
}
```


Borrowing the owner in a function lets you use its value without necessarily moving the owner to the new function’s scope.

<h3>Mutable References</h3>


My default, Rust doesn’t let you modify the value of a borrowed owner. For example, the following code won’t compile:


```Rust
fn main() {
    let mut greeting = "Hello, ".to_string();
    add_name(&greeting);
    println!("{}!", greeting);
}

fn add_name(greeting: &String) {
    greeting.push_str("John");
}
```


To make the above example work, you need to retrieve a mutable reference to the owner:


```Rust
fn main() {
    let mut greeting = "Hello, ".to_string();
    add_name(&mut greeting);
    println!("{}!", greeting);
}

fn add_name(greeting: &mut String) {
    greeting.push_str("John");
}
```


To specify a mutable reference to an owner prefix the owner name with `&mut `. Like in the 3rd line in the code above. A mutable reference allows a function to borrow and mutate an owner.

<h3>Conclusion</h3>


This article focuses on how ownership and borrowing works, and how to use them. I’ve compiled some links that can help you with any further research you may want to make:



* [References and Borrowing | Rust Docs](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
* [Understanding Ownership | Rust Docs](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html)
* [Diving into Rust’s Ownership](https://blog.knoldus.com/diving-into-rusts-ownership/)