FAQ - Help and questions

What is the difference between #[cfg(target_os)] and cfg!(target_os) in Rust?

#cfg#conditional-compilation#windows#macospublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

The #[cfg] attribute removes code that does not match before it is compiled, so macOS-only code never has to compile on Windows. The cfg! macro is just a true or false value, and both branches must compile everywhere. Use the attribute for platform APIs and the macro for small value differences.

Checked with rustc 1.93.0, Windows 11; compiled and run on Windows. The macOS function is not compiled on Windows (that is the point of the attribute) and was not tested on a Mac.

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

Rust has two ways to say "only on this system". They behave differently.

  • #[cfg(...)] is an attribute. The compiler drops the item (a function, a module, a statement) when the condition is false. The dropped code is not type-checked, so it can mention a crate or an API that only exists on the other system.
  • cfg!(...) is a macro that expands to true or false. Both branches of the if stay in the program and must compile on every system.
src/main.rsrust
// #[cfg(...)] removes code before it is compiled; cfg!(...) is a plain bool, and both branches must compile.
#[cfg(target_os = "windows")]
fn shortcut_name() -> &'static str {
    "Ctrl+,"
}

#[cfg(target_os = "macos")]
fn shortcut_name() -> &'static str {
    "Cmd+,"
}

#[cfg(not(any(target_os = "windows", target_os = "macos")))]
fn shortcut_name() -> &'static str {
    "Ctrl+,"
}

fn main() {
    println!("Settings shortcut: {}", shortcut_name());

    if cfg!(target_os = "macos") {
        println!("cfg!: running on macOS");
    } else {
        println!("cfg!: not macOS");
    }

    // Debug builds only:
    #[cfg(debug_assertions)]
    println!("debug build");
    #[cfg(not(debug_assertions))]
    println!("release build");
}
text
Settings shortcut: Ctrl+,
cfg!: not macOS
debug build

Three versions of shortcut_name exist in the source, and exactly one of them is compiled. The last one, with not(any(...)), is a safety net so that the program still builds on Linux; without it, a build for another system fails with "cannot find function shortcut_name".

When to use which:

You wantUse
to call an API that exists on one system only#[cfg(...)] on the function or module
to pick a constant or a stringcfg!(...) (or a #[cfg] pair, if the values need different types)
to add a dependency for one system only[target.'cfg(...)'.dependencies] (see the Cargo.toml thread)

cfg(windows) and cfg(unix) are shorthand for the whole family. macOS counts as unix, so cfg(unix) is true on macOS too; use target_os = "macos" when you mean only Apple's system.

Common mistakes#

  • Using if cfg!(target_os = "macos") { mac_only_call() } where mac_only_call does not exist on Windows. The if is not removed, so the build fails. Use the attribute.
  • Forgetting the fallback for other systems, so a Linux CI job fails.
  • Testing only one system. A #[cfg] branch you never compile is code nobody has checked.

How do I see which cfg values are set?#

rustc --print cfg lists them for the current target.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet