What is unsafe in Rust and when to use it

Unsafe Rust allows bypassing compiler safety checks for low-level tasks like raw pointer access and FFI, requiring manual memory safety verification.

Unsafe Rust is a block of code where you opt out of Rust's safety guarantees to perform operations like dereferencing raw pointers or calling external functions. Use it only when you must interact with C libraries, implement low-level data structures, or access mutable static variables that the safe compiler cannot verify.

unsafe {
    let ptr = 0x01234567usize as *const i32;
    let value = *ptr;
}

In Simple Terms: Unsafe Rust is like turning off the safety rails on a roller coaster to perform a specific stunt. You use it only when the standard safety features prevent you from doing something necessary, like talking to other programming languages or managing memory manually. It requires you to promise you know what you are doing, because the compiler won't stop you from crashing the program.