Skip to content

Rust Borrowing and References — References, Mutable Borrows, and Slice Types

DodaTech Updated 2026-06-28 1 min read

In this tutorial, you will learn about Rust Borrowing and References. We cover key concepts, practical examples, and best practices to help you master this topic.

Rust borrowing allows access to data without ownership transfer using immutable references, mutable references, and slice references.

What You'll Learn

  • Immutable references (&T)
  • Mutable references (&mut T)
  • Borrowing rules
  • Slice types

Why It Matters

Borrowing enables efficient data access without copying. DodaZIP uses references for buffer reading without allocation. Firefox uses borrowing for DOM traversal.

Real-World Use

Buffer reading, string processing, data structure traversal, zero-copy Parsing.

fn main() {
    // Immutable references
    let s = String::from("hello");
    let len = calculate_length(&s);
    println!("'{}' length is {}", s, len);

    // Mutable references
    let mut s = String::from("hello");
    change(&mut s);
    println!("{}", s);

    // Slice references
    let s = String::from("hello world");
    let hello = &s[0..5];
    let world = &s[6..11];
    println!("{} {}", hello, world);

    // Array slice
    let arr = [1, 2, 3, 4, 5];
    let slice = &arr[1..4];
    println!("Slice: {:?}", slice);
}

fn calculate_length(s: &String) -> usize {
    s.len()  // Borrows, doesn't own
}

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

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro