Configuration attributes

The final type of attributes are #[cfg] attributes. These attributes are incredibly powerful, enabling us to compile certain parts of the code depending on the target we are compiling to. For example, we might want to use a specific Windows function and have a backup one for the rest, or we might want the code to do different things in little- and big-endian machines.

The syntax is pretty easy. For example, if you want to check the system architecture, you can use #[cfg(target_arch = "arm")], and instead of ARM, you can check for "x86", "x86_64""mips", "powerpc", "powerpc64", or "aarch64" too. To compile something only for FreeBSD, we can use #[cfg(target_os = "freebsd")]. You can compare the target_os configuration ...

Get Rust High Performance 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.