Avatar ·

What is used in Rust instead of a garbage collector for memory management?

Hi! I just started learning Rust after Java and Python, and I have a misunderstanding. In Java and Python, there is a garbage collector (GC) that automatically frees memory when objects are no longer needed. But I read that Rust doesn't have a GC. How does Rust manage memory without a garbage collector? Do I really have to manually call `free()` or `delete`, like in C? That's inconvenient and prone to leaks! Here's a simple example I wrote trying to understand what would happen: ```rust fn main() { let s1 = String::from("Привет"); let s2 = s1; // I expected a copy, like in Java println!("{}", s1); // This doesn't compile! Error: borrow of moved value } ``` I don't understand why `s1` is no longer accessible after assigning to `s2`. In Java, `s2` would just point to the same string, and `s1` would st