Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

What is Rust Borrowing?

Author: Mansoor Ahmed
by Mansoor Ahmed
Posted: Nov 09, 2020

Borrowing

We call having references as function parameters borrowing. As in real world, if an individual owns something, you'll borrow it from them. When you’re done, you've got to offer it back.

Referencing

Reference is that the act of consulting someone or something so as to urge information

In terms of Rust programming we will define reference as if we are taking a replica of it without damaging or taking its ownership.

  • symbol is employed to pass the reference

Let’s have a glance how ‘&’ symbol is employed in code.

fn main() {

let s1 = String::from("hello");

let len = calculate_length(&s1);

println!("The length of {} is {}.", s1, len);

}

fn calculate_length(s: &String) -> usize {

s.len()}

Mutable References
  • The concept of mutable reference is same as we use mutable variable
  • Mutable reference is employed once we need to modify the worth we make regard to.
  • There should be "mut" keyword with the reference also because the variable or type we are making reference.
fn main() {

let s = String::from("hello");

change(&s);}

change(some_string: &String) {

some_string.push_str(", world");

}

  • If we attempt to run this code, we'll get a mistake because references are immutable by default in Rust.
  • We can fix that error by adding ‘mut’ keyword
fn main() {

let mut s = String::from("hello");

change(& mut s); }

fn change(some_string: & mut String) {

some_string.push_str(", world");

}

Restriction!

  • But Mutable References have one big restriction.You can have just one mutable regard to a specific piece of knowledge during a particular scope.
For Example:

let mut s = String::from("hello");

let r1 = &mut s;

let r2 = &mut s;

println!("{}, {}", r1, r2);

This is not allowed. Duplicate

Restriction Benefit

  • The advantage of having this restriction is that Rust can prevent Data Race at compile time.
Data Race Occurs when,
  • Two or more pointers access an equivalent data at an equivalent time.
  • At least one among the pointers is getting used to write down to the info.
  • There’s no mechanism getting used to synchronize access to the info.
fn main() {

let mut s = String::from("hello");

{

let r1 = &mut s;

}

r1 goes out of scope here, so we will make a replacement reference with no problems.

let r2 = &mut s;

}

fn main() {

let mut s = String::from("hello");

let r1 = &s; no problem

let r2 = &s; no problem

let r3 = &mut s; BIG PROBLEM

println!("{}, {}, and {}", r1, r2, r3);

}

Data Race

Dangling Reference
  • Dangling pointer that references a location in memory which will are given to somebody else. In Rust the compiler will make sure that the info won't leave of scope before the regard to the info does.

fn main() {

let reference_to_nothing = dangle();

}

fn dangle() -> &String {

let s = String::from("hello");

&s

}

error[E0106]: missing lifetime specifier

  • main.rs:5:16

|

5 | fn dangle() -> &

s is a new String

we return a reference to the String, s

Here, s goes out of scope, and is dropped. Its

memory goes away. Danger!

fn dangle() -> String {

let s = String::from("hello");

s

}

Rules of Reference
  • There can either be one mutable reference or any number of immutable references.
  • References should be valid.
Summary
  • Ownership and borrowing ensure memory safety at compile time.
  • Control over your memory usage within the same way as other systems programming languages.
  • Automatically pack up that data when the owner goes out of scope.
  • No extra code to urge this control.
About the Author

Mansoor Ahmed Chemical Engineer,Web developer

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Mansoor Ahmed

Mansoor Ahmed

Member since: Oct 10, 2020
Published articles: 124

Related Articles