codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🦀

Rust

29 / 58 topics
29Memory Management30Smart Pointers31Unsafe Rust
Tutorials/Rust/Memory Management
🦀Rust

Memory Management

Updated 2026-05-15
10 min read

Memory Management

Introduction

Rust is a systems programming language that emphasizes safety, speed, and concurrency. One of its core features is memory safety without a garbage collector. This tutorial will delve into how Rust achieves memory safety through its ownership model, borrowing rules, and lifetimes.

Concept

Ownership Model

At the heart of Rust's memory management is the ownership model. Every value in Rust has an owner, and there can only be one owner at a time. When the owner goes out of scope, the value is dropped, meaning its memory is automatically deallocated.

Rules of Ownership:

  1. Each value in Rust has a variable that’s called its owner.
  2. There can only be one owner at a time.
  3. When the owner goes out of scope, the value will be dropped.

Borrowing

Borrowing allows you to refer to another value without taking ownership of it. References are immutable by default, but you can also have mutable references.

Rules of Borrowing:

  1. At any given time, you can have either one mutable reference or any number of immutable references.
  2. References must always be valid.

Lifetimes

Lifetimes ensure that references always point to valid data. They are a way of telling the Rust compiler how long references are valid.

Examples

Let's explore these concepts with some practical examples.

Example 1: Ownership

fn main() {
    let s1 = String::from("hello");
    let s2 = s1; // Ownership of s1 is moved to s2

    println!("{}", s2); // This will work
    // println!("{}", s1); // This will cause a compile-time error
}

In this example, `s1` is moved into `s2`, so trying to use `s1` after that results in a compile-time error.

### Example 2: Borrowing

```rust
fn main() {
    let mut s = String::from("hello");

    change(&mut s); // Mutable borrow of s

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

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

Here, we have a mutable reference to `s`, allowing us to modify it inside the `change` function.

### Example 3: Lifetimes

```rust
fn main() {
    let string1 = String::from("abcd");
    let string2 = "xyz";

    let result = longest(string1.as_str(), string2);
    println!("The longest string is {}", result);
}

fn longest(x: &str, y: &str) -> &str {
    if x.len() > y.len() {
        x
    } else {
        y
    }
}

In this example, the `longest` function returns a reference to either `x` or `y`. The Rust compiler infers the lifetime of the returned reference based on the input lifetimes.

## What's Next?

Understanding memory management in Rust is crucial for writing safe and efficient code. In the next section, we will explore "Smart Pointers," which provide more control over memory allocation and deallocation.

By mastering these concepts, you'll be well-equipped to write robust and high-performance applications in Rust.

PreviousTokio RuntimeNext Smart Pointers

Recommended Gear

Tokio RuntimeSmart Pointers