Rust
December 2, 2026 6 min read

Borrowing and References in Rust

HS
HEMANT SINGH
Technical Writer & Educator

1. What is Borrowing?

Borrowing means accessing a value without taking ownership of it.

Instead of transferring ownership, we pass a reference to the value.

Example Without Borrowing

fn display_name(name: String) {
println!("Name: {}", name);
}

fn main() {
let name = String::from("Hemant");

display_name(name);

// name cannot be used here
}

Here, ownership moves from main() to display_name().

Example With Borrowing

fn display_name(name: &String) {
println!("Name: {}", name);
}

fn main() {
let name = String::from("Hemant");

display_name(&name);

println!("Original Name: {}", name);
}

Output:

Name: Hemant
Original Name: Hemant

Here:

  1. name owns the original string.
  2. &name creates a reference to the string.
  3. The function borrows the value.
  4. Ownership remains with the original variable.

Important: Borrowing allows us to use a value without taking ownership of it.

2. What is a Reference?

A reference is a way to refer to an existing value without owning it.

In Rust, we use the & symbol to create a reference.

Syntax

let reference = &variable;

Example

fn main() {
let number = 100;

let reference = &number;

println!("Number: {}", number);
println!("Reference: {}", reference);
}

Output:

Number: 100
Reference: 100

Both print the same value.

However:

  1. number is the original value.
  2. reference refers to that value.

3. Immutable References

An immutable reference allows us to read a value but not modify it through that reference.

Example

fn main() {
let name = String::from("Hemant");

let reference = &name;

println!("{}", reference);
}

Output:

Hemant

Here, reference can access the string, but cannot modify it.

Multiple Immutable References

Rust allows multiple immutable references to the same value at the same time.

Example:

fn main() {
let name = String::from("Hemant");

let ref1 = &name;
let ref2 = &name;
let ref3 = &name;

println!("{}", ref1);
println!("{}", ref2);
println!("{}", ref3);
}

Output:

Hemant
Hemant
Hemant

This is allowed because all three references only read the value.

4. Mutable References

A mutable reference allows us to modify the original value.

We use &mut to create a mutable reference.

Syntax

let reference = &mut variable;

Example

fn main() {
let mut name = String::from("Hello");

let reference = &mut name;

reference.push_str(" Rust");

println!("{}", reference);
}

Output:

Hello Rust

Here:

  1. name is declared using mut.
  2. &mut name creates a mutable reference.
  3. push_str() modifies the original string through the reference.

Important

To create a mutable reference, the original variable must also be mutable.

Incorrect:

fn main() {
let name = String::from("Hello");

let reference = &mut name;
}

This produces a compilation error because name is not mutable.

Correct:

fn main() {
let mut name = String::from("Hello");

let reference = &mut name;

reference.push_str(" Rust");

println!("{}", name);
}

5. Borrowing Rules in Rust

Rust follows important rules to prevent data races and unsafe memory access.

Rule 1: Multiple Immutable References Are Allowed

fn main() {
let number = 100;

let a = &number;
let b = &number;
let c = &number;

println!("{} {} {}", a, b, c);
}

This is allowed.

Rule 2: Only One Active Mutable Reference at a Time

fn main() {
let mut number = 100;

let a = &mut number;

*a += 10;

println!("{}", a);
}

This is allowed.

But creating two simultaneously usable mutable references to the same value is not allowed.

Incorrect:

fn main() {
let mut number = 100;

let a = &mut number;
let b = &mut number;

println!("{} {}", a, b);
}

This produces a compilation error because two mutable references overlap.

Rule 3: Mutable and Immutable References Cannot Overlap

Rust does not allow an immutable reference and a mutable reference to the same value to be used at the same time.

Incorrect:

fn main() {
let mut number = 100;

let a = &number;
let b = &mut number;

println!("{} {}", a, b);
}

This causes a compilation error because the references overlap.

Note: Rust checks when references are actually used. Non-lexical lifetimes allow a borrow to end after its last use, even if the variable remains in scope.

6. Understanding Dereferencing (*)

The * operator is called the dereference operator.

It allows us to access the value behind a reference.

Example

fn main() {
let number = 100;

let reference = &number;

println!("Reference: {}", reference);
println!("Dereferenced: {}", *reference);
}

Output:

Reference: 100
Dereferenced: 100

Both print the same value, but they represent different operations.

  1. reference is a reference.
  2. *reference accesses the value it refers to.

Modifying Through a Mutable Reference

fn main() {
let mut number = 10;

let reference = &mut number;

*reference = 50;

println!("{}", number);
}

Output:

50

Here, *reference = 50 modifies the original value.

7. References in Functions

References are commonly used as function parameters.

This allows functions to use values without taking ownership.

Example

fn calculate_length(text: &String) -> usize {
text.len()
}

fn main() {
let name = String::from("Hemant");

let length = calculate_length(&name);

println!("Name: {}", name);
println!("Length: {}", length);
}

Output:

Name: Hemant
Length: 6

Here:

  1. &String means the function accepts a reference to a String.
  2. usize is the return type of .len().
  3. Ownership remains with main().

Using &str Instead of &String

For functions that only need to read text, &str is often more flexible.

fn display_name(name: &str) {
println!("Name: {}", name);
}

fn main() {
let name1 = String::from("Hemant");
let name2 = "Rahul";

display_name(&name1);
display_name(name2);
}

Output:

Name: Hemant
Name: Rahul

The same function accepts both a borrowed String and a string slice.

8. Mutable References in Functions

A function can modify a value through a mutable reference.

Example

fn add_bonus(salary: &mut i32) {
*salary += 5000;
}

fn main() {
let mut salary = 20000;

add_bonus(&mut salary);

println!("Salary: {}", salary);
}

Output:

Salary: 25000

Here:

  1. &mut i32 means the function accepts a mutable reference to an integer.
  2. *salary accesses the original integer.
  3. The function modifies the original value without taking ownership.

9. What is a Dangling Reference?

A dangling reference is a reference that points to memory that is no longer valid.

Rust prevents references from outliving the values they refer to.

Incorrect Example

fn main() {
let reference;

{
let number = 100;

reference = &number;
}

println!("{}", reference);
}

This code produces a compilation error.

Why?

  1. number is declared inside the inner block.
  2. When the block ends, number goes out of scope.
  3. reference would point to a value that no longer exists.

Rust prevents this problem at compile time.

10. Difference Between Ownership and Borrowing


Feature

Ownership

Borrowing

Meaning

Controls who owns a value

Allows access without ownership transfer

Symbol

No special symbol

& or &mut

Ownership transfer

Can transfer ownership

Does not transfer ownership

Modification

Owner can modify if mutable

Mutable reference can modify

Example

let b = a;

let b = &a;

11. Quick Reference


Concept

Syntax

Purpose

Immutable reference

&value

Read a value

Mutable reference

&mut value

Modify a value

Dereference

*reference

Access the referenced value

Function borrowing

fn show(x: &String)

Accept a borrowed String

Mutable function parameter

fn update(x: &mut i32)

Modify a borrowed integer

String slice parameter

fn show(x: &str)

Accept borrowed text