Bedingte Kompilierung

Rust hat ein spezielles Attribut, #[cfg], welches es uns erlaubt Code nur dann kompilieren zu lassen wenn ein bestimmtes Flag an den Compiler übergeben wird. Davon gibt es zwei Formen:

#[cfg(foo)] #[cfg(bar = "baz")]

Dann gibt es noch Zusätze wie any(), all() oder not():

#[cfg(any(unix, windows))] #[cfg(all(unix, target_pointer_width = "32"))] #[cfg(not(foo))]

Und diese können wiederum beliebig geschachtelt werden:

#[cfg(any(not(unix), all(target_os="macos", target_arch = "powerpc")))]

Um diese Schalter an und auszuschalten, benutzt du in deiner Cargo.toml die [features] Sektion (englisch):

[features]
# no features by default
default = []

# The “secure-password” feature depends on the bcrypt package.
secure-password = ["bcrypt"]

Wenn das passiert ist übergibt Cargo die Flags auf diese Weise an rustc:

--cfg feature="${feature_name}"

Die Summe dieser cfg Flags bestimmt welche aktiviert und damit kompiliert werden. Hier am Beispiel:

#[cfg(feature = "foo")] mod foo { }

Wenn wir jetzt mit cargo build --features "foo" kompilieren, wird das Flag --cfg feature="foo" an rustc gesendet und das Ergebnis wird mod foo enthalten. Wenn wir allerdings normal mit cargo build kompiliren, ohne extra Flags, gibt es kein foo.

cfg_attr

Man kann auch andere Attribute basierend auf cfg Variablen mit cfg_attr setzen:

#[cfg_attr(a, b)]

Das ist das selbe wie #[b] wenn a vom cfg Attribut gesetzt ist. Sonst nichts.

cfg!

Das cfg! Macro ist eine Syntax Erweiterung die dich diese Flags an beliebigen Stellen im Code verwenden lässt:

if cfg!(target_os = "macos") || cfg!(target_os = "ios") { println!("Think Different!"); }

Das wird zur beim Kompilieren durch true oder false ersetzt, je nach Konfiguration.