April 2026
Intermediate
631 pages
16h 20m
English
Rust is renowned for being a memory-safe language, offering strong guarantees against data races. However, Rust does not provide equally strict guarantees when it comes to memory leaks, that is, situations where memory is not properly freed up. While challenging in Rust, you can create memory that is never deallocated. Memory leaks can occur, particularly when using Rc and RefCell smart pointers, the latter of which enables mutability.
To demonstrate, let’s define a Node struct and its Drop implementation, as shown in Listing 11.32.
use std::cell::RefCell;use std::rc::Rc;#[derive(Debug)]struct Node { next: Option<Rc<RefCell<Node>>>,}impl Drop for Node { fn drop(&mut self) { println!("Dropping ...
Read now
Unlock full access