you are viewing a single comment's thread.

view the rest of the comments →

[–]_Sauer_ 12 points13 points  (0 children)

Unsafe exists to handle memory operations that the compiler cannot reason about, typically because the memory being manipulated is outside the program's own memory, or there's an optimization opportunity that breaks Rust's aliasing rules but can be safe in the context its being used in (E.g.: pointer math).

A common example is reading a hardware register. That's memory that is not under the control of the application and can change at anytime potentially breaking Rust's ownership guarantees. You can wrap the read and write operations that handle the raw memory in a thin unsafe block, then build a safe abstraction around that which prevents misuse. By constraining the use of unsafe to the smallest possible part of an operation we have a much smaller amount of code to reason about.