Specialization

In Chapter 2, Sequential Rust Performance and Testing, we noted that the technique of specialization would be cut off from data structure implementors if they intended to target stable Rust. Specialization is a means by which a generic structure—say, a HashMap—can be implemented specifically for one type. Usually, this is done to improve performance—say, by turning a HashMap with u8 keys into an array—or provide a simpler implementation when possible. Specialization has been in Rust for a good while, but limited to nightly projects, such as the compiler, because of soundness issues when interacting with lifetimes. The canonical example has always been the following:

trait Bad1 { fn bad1(&self); } impl<T> Bad1 for T { default ...

Get Hands-On Concurrency with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.