Rise In Logo





Build on Stellar

Borrowing and References

Borrowing and references are fundamental concepts in Rust's memory management system. In Rust, ownership ensures that each value has exactly one owner, which helps to prevent common memory-related bugs. 

However, there are cases where you need to access a value without taking ownership of it. This is where borrowing and references come in.

Borrowing allows you to temporarily access a value without taking ownership of it. When you borrow a value, you create a reference to it, which allows you to read or modify the value without becoming the owner. References are lightweight and do not carry the same overhead as owned values, which makes them a useful tool for writing efficient code in Rust. Now, let's dive into the details of borrowing.

In Rust, you can create a borrow by using a reference. A reference is a pointer to a value, but unlike a raw pointer in C, a reference is guaranteed to be valid and safe by the compiler. There are two types of references in Rust: mutable references and immutable references.

Immutable References

Immutable references are references that allow you to read a value, but not modify it. You create an immutable reference by using the & operator followed by the variable name. For example, let's say we have a string:

let my_string = String::from("hello, world!");

We can create an immutable reference to my_string like this:

let my_ref = &my_string;

Now my_ref is an immutable reference to my_string, which means that we can read the value of my_string through my_ref, but we can't modify my_string through my_ref.

Let's see an example of using an immutable reference in a function:

fn print_string(s: &String) {
  println!("{}", s);
}


let my_string = String::from("hello, world!");
print_string(&my_string);

In this example, we've defined a function called print_string that takes an immutable reference to a String as an argument. We then create a String called my_string, and pass an immutable reference to it to the print_string function. The print_string function can read the value of my_string through the immutable reference, but it can't modify my_string itself.

One important thing to note about immutable references is that you can have multiple immutable references to the same value at the same time. This is known as "aliasing", and it allows you to read the value from different parts of your code without causing any issues. 

However, you can't have a mutable reference and an immutable reference to the same value at the same time, as this would violate Rust's borrowing rules.

To summarize, immutable references in Rust allow you to read a value without modifying it. You can create an immutable reference by using the & operator followed by the variable name. You can pass immutable references to functions to allow them to read values without taking ownership of them. And you can have multiple immutable references to the same value at the same time, but you can't have a mutable reference and an immutable reference to the same value at the same time.

Mutable References

In Rust, a mutable reference is a reference to a variable that allows you to modify the value that it points to. Unlike an immutable reference, which only allows you to read the value, a mutable reference allows you to both read and write the value.

To create a mutable reference in Rust, you use the &mut syntax. Here's an example:

fn main() {
  let mut my_string = String::from("hello");


  change_string(&mut my_string);


  println!("{}", my_string); // prints "hello world"
}


fn change_string(s: &mut String) {
  s.push_str(" world");
}

In this example, we create a mutable reference to the my_string variable using &mut. We then pass that reference to the change_string function, which appends the string " world" to the end of my_string. Finally, we print the value of my_string to the console, which now contains the value "hello world".

It's important to note that you can only have one mutable reference to a variable at a time. This is to prevent data races and other types of undefined behavior that can occur when multiple threads try to modify the same variable at the same time. If you try to create a second mutable reference to the same variable before the first one goes out of scope, you'll get a compile-time error.

However, as we talked earlier, we can have as many immutable references to a variable as you want, as long as there are no mutable references. This is because immutable references only allow you to read the value, not modify it, so there's no risk of data races or other issues.

Here's an example that demonstrates this:

fn main() {
  let mut my_string = String::from("hello");


  let r1 = &my_string; // immutable reference
  let r2 = &my_string; // another immutable reference


  println!("{}, {}", r1, r2); // prints "hello, hello"


  let r3 = &mut my_string; // mutable reference


  r3.push_str(" world");


  println!("{}", r3); // prints "hello world"
}


In this example, we create two immutable references to my_string, which allows us to read the value of the variable without modifying it. We then create a mutable reference to my_string using &mut, which allows us to modify the value of the variable. Finally, we print the value of the mutable reference to the console, which now contains the value "hello world".

Now, here is a quick reference guide for borrowing rules:

  1. You can have either one mutable reference or any number of immutable references to a variable at a time. 
  2. You cannot have both a mutable and an immutable reference to the same variable at the same time. 
  3. References must always be valid and point to a valid memory location. 
  4. References automatically expire at the end of their scope. 

By following these rules, you can ensure that your code is safe and free of common bugs that can arise from mutable state and concurrent access to shared data.

Dangling References

In Rust, a dangling reference is a reference that points to a memory location that has been deallocated, causing unexpected behavior or a runtime error. This can happen when a reference is still in scope after the object it refers to has been dropped.

Here's an example of how a dangling reference can occur in Rust:

fn return_reference(some_string: &String) -> &String {
  some_string
} // some_string goes out of scope, but reference_to_s still points to the memory location

In this example, we create a new String called s and pass a reference to it into the return_reference function. The function then returns a reference to the same String. However, when main exits, s goes out of scope and gets deallocated. This means that the reference returned by return_reference is now pointing to invalid memory, which can cause a runtime error or other unexpected behavior.

In conclusion, borrowing and references are powerful features of Rust that allow for safe and efficient memory management. By using borrowing, Rust ensures that only one owner has mutable access to a piece of data at a time, preventing data races and other common errors. 

Additionally, by using references, Rust allows for non-owning access to data, which can be useful for passing data between functions or for allowing multiple parts of a program to access the same data without copying it.

However, it's important to keep in mind the borrowing rules in Rust, which include rules for mutable and immutable references and rules for borrowing in structs and enums. By following these rules and understanding the concepts of borrowing and references, you can write safer, more efficient, and more reliable Rust code.

Rise In Logo

Rise together in web3