What Are Smart Pointers in Rust?

Smart pointers in Rust are types that own data, implement custom behavior via traits, and manage memory automatically.

Smart pointers in Rust are types that act like pointers but also own the data they point to and implement additional functionality like automatic memory management. They implement the Deref trait to behave like references and the Drop trait to clean up resources when they go out of scope.

use std::rc::Rc;

fn main() {
    let data = Rc::new(String::from("Hello"));
    let clone = Rc::clone(&data);
    println!("{}", data); // Automatically dereferences
}