1 Month of Learning Rust - Data Mutability and Borrowing Ownership

1 Month of Learning Rust - Data Mutability and Borrowing Ownership

Daily short news for you
  • swapy is a library that helps you create drag-and-drop actions to easily swap the positions of components.

    The library supports a wide range of platforms, making it perfect for building something personalized like a user’s Dashboard.

    » Read more
  • After waking up, I saw the news feed flooded with articles about Microsoft rewriting the TypeScript compiler - tsc in Go, achieving performance that is 10 times faster than the current one. Wow!

    But when I saw this news, the question immediately popped into my head: "Why not Rust?" You know, the trend of rewriting everything in Rust is hotter than ever, and it’s not an exaggeration to say that it's sweeping through the rankings of previous legacy tools.

    What’s even more interesting is the choice of Go - which supposedly provides the best performance to date - as they say. Many people expressed disappointment about why it wasn’t C# instead 😆. When something is too famous, everything is scrutinized down to the smallest detail, and not everyone listens to what is said 🥶

    Why Go? #411

    » Read more
  • I read this article Migrating Off Oh-My-Zsh and other recent Yak Shavings - the author essentially states that he has been using Oh-My-Zsh (OMZ) for a long time, but now the game has changed, and there are many more powerful tools that have come out, so he realizes, oh, he doesn't need OMZ as much anymore.

    I also tried to follow along because I find this situation quite similar to the current state. It was surprising to discover something new. I hope to soon write a post about this next migration for my readers 😁

    » Read more

The Issue

In the previous article, we learned about the concept of ownership in Rust. Thanks to ownership, Rust knows when a variable is no longer in use and can free up memory. By using & before the variable name, we declare to Rust that we're just "borrowing" the value temporarily without transferring ownership, so the borrowed variable still exists after borrowing, but with some limitations.

Today's article will delve into how Rust deals with data borrowing behavior and how it prevents "non-standard" behaviors with borrowed and mutable data during compile time, not runtime.

Rust Avoids Borrowing and Mutating Data Simultaneously

First, let's take a look at how Rust handles memory in this example program.

let mut v: Vec<i32> = vec![1, 2, 3];
v.push(4);

Rust Avoids Borrowing and Mutating Data Simultaneously

At line L2, adding an element to a list causes a mutation, but instead of adding 4 to the next memory location, Rust copies the entire data to another memory location, causing the data in the heap of v at L1 to be freed, and v now points to a different memory location.

What would happen with the following program?

let mut v: Vec<i32> = vec![1, 2, 3];
let num: &i32 = &v[2];
v.push(4);
println!("Third element is {}", *num);

Here, num is borrowing the value at position 2 of v through &v[2]. But after the push operation, the original heap data of v has been deallocated. So, where does num point to? You guessed it right; this program would result in an error.

Rust Avoids Borrowing and Mutating Data Simultaneously - 2

Therefore, Rust enforces the safety principle of pointers: data can never be both borrowed and mutated at the same time.

To introduce borrowing, Rust has established rules to ensure the "Pointer Safety Principle," which is enforced by the borrow checker tool.

Borrow Checker

The core idea behind Rust is the concept of three ownership rights for a variable's data.

  • Read (R): Data can be copied to another location.
  • Write (W): Data can be modified.
  • Own (O): Data can be moved or deleted.

These rights only exist at compile time, meaning the compiler "independently derives" these rights to check whether your program is valid before building it into binary.

By default, a variable has R and O rights over its data. If a variable is declared as mut, it gains W rights.

Let's consider how Rust checks data rights in the following program.

Rust Checks Rights

First, v is declared as mut, so it has R, O, and W rights. Then, num borrows the value of v, and now v loses 2 rights: W and O, while num gains R and O rights over the borrowed data. *num is a variable reading the value at the location it borrowed, so it only has R rights.

Right after the println! statement, num is released, restoring all rights to v, but num and *num lose all rights to the borrowed data.

After the push statement, v is no longer in use, so it also loses all rights to its data.

Rust calls everything to the left of the assignment (=) as paths. Thus, ownership rights are determined on paths, not just on variables. Paths include:

  • Variables, like a.
  • *a.
  • Accessing elements in an array: a[0].
  • a.0 of tuples or a.field of structs.
  • And some more complex accesses like *((*a)[0].1)!?.

So, can a variable borrow data from another variable and modify that data? Remember the concept: "Data can never be both borrowed and mutated at the same time."

let mut v: Vec<i32> = vec![1, 2, 3];
let num: &mut i32 = &mut v[2];
*num += 1;
println!("Third element is {}", *num);
println!("Vector is now {:?}", v);

Instead of &v[2], we now declare &mut v[2] to indicate that num has ownership rights (O) over the borrowed data. The variable *num directly reads data from the heap and increments it by 1. In the end, v is also modified according to num.

Compared to the initial example, v loses all rights when num borrows data with O rights. This ensures data safety and adheres to the principle of "Data can never be both borrowed and mutated at the same time."

Data Must Outlive Its References

Let's start with an example:

let s = String::from("Hello world");
let s_ref = &s;
drop(s);
println!("{}", s_ref);

The code is attempting to drop s while s_ref is borrowing its value. Remember the concept mentioned earlier: "Data can never be both borrowed and mutated at the same time." So, drop cannot proceed because it requires O rights. This is why the program results in an error.

Both we and Rust can quickly identify the lifetime of s. However, programs are not always straightforward, and Rust needs a way to determine the lifetime of a variable in the program to prevent situations like the one above.

Consider this function:

fn first_or(strings: &Vec<String>, default: &String) -> &String {
    if strings.len() > 0 {
        &strings[0]
    } else {
        default
    }
}

The function can return either the first String from the strings parameter or the default parameter. What would happen if we call the function like this?

fn main() {
    let strings = vec![];
    let default = String::from("default");
    let s = first_or(&strings, &default);
    drop(default);
    println!("{}", s);
}

Since strings is empty, s will borrow from default. However, a drop(default) statement occurs before printing s. Therefore, this program is not safe and will result in an error.

Another example of an unsafe program:

fn return_a_string() -> &String {
    let s = String::from("Hello world");
    let s_ref = &s;
    s_ref
}

s_ref is attempting to borrow the value of s, but when the function ends, s will be deallocated, making returning s_ref meaningless. Hence, this program will result in an error.

Conclusion

This concludes the article on ownership rights and how Rust checks borrowing data rights. In my opinion, this is one of the most challenging concepts in Rust, but it's crucial for writing and debugging programs. Readers can continue reading the article Fixing Ownership Errors to learn how to resolve some common ownership-related errors.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (2)

Leave a comment...
Avatar
Ẩn danh1 year ago
"lúc này v bị mất 2 quyền W, O, bù lại num có quyền R, O trên dữ liệu mà nó mới mượn được". References không chiếm quyền sở hữu, nó chỉ mượn (borrowing), giống như biến, ref cũng có 2 kiểu mượn là immutable vs mutable nhưng có phụ thuộc vào owner, nếu owner khai bao với mutable thì ref với được mutable
Reply
Avatar
Ẩn danh1 year ago
"Tại L2, hành động thêm một phần tử vào một danh sách đã gây nên đột biến, nhưng thay vì thêm 4 vào ô nhớ tiếp theo thì Rust đã sao chép toàn bộ dữ liệu sang một vùng nhớ khác". chỗ này không phải lúc nào cũng cũng được cấp phát mới vùng nhớ, nó phục thuộc vào cái cap của Vec, khi len > cap thì Vec mới được cấp phát với cap mới là x2, vì vậy để tối ưu chương trình nếu biết len của Vec cần sử dụng thì thường người ta sẽ khởi tạo nó với cap được xác định trước để tránh bị cấp phái lại nhiều lần
Reply