Avatar ·

What does Rust use instead of a garbage collector? GC alternatives in Rust

Hello, colleagues! I'm switching to Rust after several years of working with Java and Python, and I'm struggling with one fundamental question. In Java, I'm used to the garbage collector (GC) automatically freeing memory when objects become unreachable. In Python — similarly, reference counting and GC for cyclic references. But Rust is fundamentally different: there's no garbage collector here, yet the language is considered safe. I wrote a simple example that would work without issues in Java or Python, but raises questions in Rust: ```rust struct User { name: String, posts: Vec, } fn main() { let user = User { name: "Alice".to_string(), posts: vec!["Hello".to_string()], }; // How does Rust know when to free memory for user? // What happens if I w