Improving performance using unsafe Rust code
While we should prefer safe Rust code whenever possible, some optimizations may only present themselves in an unsafe context. Unsafe Rust code is exactly what it sounds like... unsafe. There are many challenges to working with unsafe Rust, and it requires a much greater understanding of the language to use it correctly. We'll only be examining some small parts of the unsafe Rust universe in this section. For a much larger treatment of the subject, the best source of reference is The Rustnomicon, available at https://doc.rust-lang.org/nomicon/.
Dropping into unsafe code to improve performance is not always necessary, but there are a handful of times when it can help push us over the line for a really critical section of code. Armed with our benchmarking setup and matrix multiplication example from the previous section, let's dive into the world of unsafe Rust and examine two specific ways it can help us optimize our code.
...