Borrowing in Rust

Borrowing is a key concept in Rust that allows you to access data without taking ownership of it. This mechanism enables multiple parts of your program to read or modify data safely while ensuring memory safety and preventing data races. Borrowing is distinct from ownership, which refers to the concept of having exclusive control over a value.

1. Ownership vs. Borrowing

Ownership in Rust means that a variable is responsible for managing the memory of the value it holds. When a variable goes out of scope, Rust automatically cleans up the memory associated with that value. In contrast, borrowing allows you to temporarily use a value without taking ownership, which means the original owner retains control over the value.

Key Differences

  • Ownership: A value can have only one owner at a time. When ownership is transferred, the original owner can no longer access the value.
  • Borrowing: You can create references to a value, allowing multiple parts of your code to access it without taking ownership. Borrowing can be either immutable or mutable.

2. Immutable Borrowing

Immutable borrowing allows you to create references to a value without modifying it. You can have multiple immutable references to the same value at the same time, which is safe because they cannot change the data they point to.

Example of Immutable Borrowing


fn main() {
let s1 = String::from("Hello, Rust!");
let len = calculate_length(&s1); // Borrowing s1 immutably

println!("The length of '{}' is {}.", s1, len); // s1 can still be used
}

fn calculate_length(s: &String) -> usize {
s.len() // Returns the length of the string
}

Explanation of the Example

  • In this example, we create a String and assign it to the variable s1.
  • We then pass an immutable reference of s1 to the calculate_length function using &s1.
  • The function takes a reference to a String and returns its length without taking ownership of the string.
  • After the function call, we can still use s1 because we only borrowed it immutably.

3. Mutable Borrowing

Mutable borrowing allows you to create a reference to a value and modify it. However, Rust enforces strict rules: you can have either one mutable reference or multiple immutable references to a piece of data at a time. This prevents data races and ensures memory safety.

Example of Mutable Borrowing


fn main() {
let mut s1 = String::from("Hello");
change(&mut s1); // Borrowing s1 mutably

println!("{}", s1); // Output: "Hello, world!"
}

fn change(s: &mut String) {
s.push_str(", world!"); // Modifying the borrowed string
}

Explanation of the Example

  • In this example, we declare s1 as mutable using mut.
  • We borrow s1 mutably by passing a mutable reference to the change function using &mut s1.
  • Inside the change function, we can modify the borrowed string, and the changes will be reflected in the original variable.
  • Rust ensures that no other references (mutable or immutable) exist while we have a mutable reference, preventing data races.

4. Borrowing Rules

Rust enforces several rules regarding borrowing to ensure memory safety:

  • You can have either one mutable reference or any number of immutable references to a value at a time.
  • References must always be valid. If the owner of a value goes out of scope, any references to that value become invalid.

5. Conclusion

Borrowing is a powerful feature in Rust that allows for safe access to data without transferring ownership. By understanding the differences between ownership and borrowing, as well as the rules governing them, you can write more efficient and safe Rust code. Borrowing enables you to share data across different parts of your program while maintaining memory safety and preventing data races.