Why does Rust have ownership instead of garbage collection

Rust uses ownership to ensure memory safety and prevent data races at compile time without the performance cost of garbage collection.

Rust uses ownership to guarantee memory safety and prevent data races without the performance overhead of a garbage collector. The compiler enforces strict rules at compile time, ensuring memory is freed immediately when variables go out of scope rather than waiting for a runtime process. This design allows Rust to offer C-like performance with high-level safety guarantees.

fn main() {
    let s = String::from("hello");
    // Memory for 's' is automatically freed here when it goes out of scope
}