Env Consts Reveal The Target Platform

Read OS, ARCH, FAMILY, and linker suffixes from env::consts so install paths and binary names follow the target platform.

RustCli Tooling

Code

rustsnippet
16 lines
use std::env::consts::{ARCH, DLL_SUFFIX, EXE_SUFFIX, FAMILY, OS};

fn main() {
    // compile-time constants: the toolchain bakes in the target platform
    println!("os: {OS}");
    println!("arch: {ARCH}");
    println!("family: {FAMILY}");

    // suffixes decide binary names: main vs main.exe, libfoo vs foo.dll
    println!("exe suffix: '{EXE_SUFFIX}'");
    println!("dll suffix: '{DLL_SUFFIX}'");

    // runtime branch keyed on the platform family
    let separator = if FAMILY == "windows" { '\\' } else { '/' };
    println!("path separator: {separator}");
}

How the snippet works

Env Consts Reveal The Target Platform is built around the function main.

Applying it

The demo output is labeled (os:, arch:, family:); each label names the call that printed it.

Statements that matter

let separator = if FAMILY == "windows" { '\\' } else { '/' };

Interactive Tools for This Topic

FAQ

What does Env Consts Reveal The Target Platform print when run?
Running the file directly is enough — expect labeled output lines such as `os:`, `arch:`, `family:`.