Categories
Safety Equipment and Gear

Safety off: Programming in Rust with `unsafe` [Video]

What does it mean to write unsafe code in Rust, and what can you do (and not do) with the ‘unsafe’ keyword? The facts may surprise you.

Credit: Reddogs / Shutterstock

Reasons abound for Rust’s growing popularity: it’s fast, memory-safe without needing garbage collection, and outfitted with world-class tooling. Rust also allows experienced programmers to selectively toggle off some—although not all—of its safeties for the sake of speed or direct low-level memory manipulation.

“Unsafe” Rust is the general term for any Rust code that is contained in a block delineated by the unsafe keyword. Inside an unsafe block, you can bend (but not break) some (but not all) of Rust’s safety rules.

If you’ve come from C or another low-level systems language, you may be tempted to reach for unsafe whenever you want to use some familiar pattern for manipulating things in a low-level way. In some cases, you may be right: there are a few things you can’t do in Rust except through unsafe

Watch/Read More