2.12 Memory Safety and Ownership
While not deeply covered in this chapter, it's essential to recognize that Rust's ownership model ensures memory safety without a garbage collector.
- Ownership: Each value in Rust has a variable that's its owner.
- Borrowing: References allow you to borrow data without taking ownership.
- No Null: Rust does not have null pointers; instead, it uses
Option<T>
to represent optional values.
2.12.1 Comparison with C
- C requires manual memory management with
malloc
andfree
. - Null pointers can lead to segmentation faults.
- Rust prevents common errors like use-after-free and null dereferencing at compile time.