pub struct Target { /* private fields */ }Expand description
A container for all lines from the output that are prefixed with the
target_ string.
For more information about possible values and recognized key-value pairs, see the Rust Reference book on Conditional Compilation.
Implementations§
Source§impl Target
impl Target
Sourcepub fn arch(&self) -> &str
pub fn arch(&self) -> &str
The target’s CPU architecture.
This is the target_arch line in the output. See the target_arch
section on Conditional Compilation in the Rust Reference book for
example values. The surrounding double quotes, ", of the raw output of
the cargo rustc -- --print cfg command are removed.
§Examples
For a 32-bit Intel x86 target:
let cfg = Cfg::rustc_target("i686-pc-windows-msvc")?;
assert_eq!(cfg.target().arch(), "x86");For a 64-bit Intel x86 target:
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
assert_eq!(cfg.target().arch(), "x86_64");For a 32-bit ARM target:
let cfg = Cfg::rustc_target("thumbv7a-pc-windows-msvc")?;
assert_eq!(cfg.target().arch(), "arm");For a 64-bit ARM target:
let cfg = Cfg::rustc_target("aarch64-pc-windows-msvc")?;
assert_eq!(cfg.target().arch(), "aarch64");Sourcepub fn endian(&self) -> &str
pub fn endian(&self) -> &str
The target’s CPU endianness.
This is the target_endian line in the output. See the
target_endian section on Conditional Compilation in the Rust
Reference book for example values. The surrounding double quotes, ",
of the raw output of the cargo rustc -- --print cfg command are
removed.
§Examples
For a little endian target:
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
assert_eq!(cfg.target().endian(), "little");For a big endian target:
let cfg = Cfg::rustc_target("sparc64-unknown-linux-gnu")?;
assert_eq!(cfg.target().endian(), "big");Sourcepub fn env(&self) -> Option<&str>
pub fn env(&self) -> Option<&str>
The Application Binary Interface (ABI) or libc used by the target.
This is the target_env line in the output. See the
target_env section on Conditional Compilation in the Rust
Reference book for example values. The surrounding double quotes, ",
of the raw output of the cargo rustc -- --print cfg command are
removed.
This will return None if the target_env line is missing from the
output or the value is the empty string, "".
§Examples
let cfg = Cfg::rustc_target("x86_64-pc-windows-gnu")?;
assert_eq!(cfg.target().env(), Some("gnu"));Sourcepub fn family(&self) -> Option<&str>
pub fn family(&self) -> Option<&str>
The target’s operating system family.
This is the target_family line in the output. See the
target_family section on Conditional Compilation in the Rust
Reference book for example values. The surrounding double quotes, ",
of the raw output of the cargo rustc -- --print cfg command are
removed.
This will return None if the target_family key-value pair was missing
from the output or the value was the empty string, "".
§Examples
For a Windows target:
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
assert_eq!(cfg.target().family(), Some("windows"));For a Linux target:
let cfg = Cfg::rustc_target("x86_64-unknown-linux-gnu")?;
assert_eq!(cfg.target().family(), Some("unix"));For an Apple target:
let cfg = Cfg::rustc_target("x86_64-apple-darwin")?;
assert_eq!(cfg.target().family(), Some("unix"));Sourcepub fn features(&self) -> Vec<&str>
pub fn features(&self) -> Vec<&str>
The features enabled for a target’s compilation.
This is any target_feature line in the output. See the
target_feature section on Conditional Compilation in the Rust
Reference book for example values. The surrounding double quotes, ",
of the raw output of the cargo rustc -- --print cfg command are
removed.
Compiler features are enabled and disabled using either the Rust
compiler’s (rustc) -C/--codegen command line option, the Cargo
rustflags key-value configuration, or the RUSTFLAGS environment
variable supported by Cargo but not rustc.
§Examples
Using the RUSTFLAGS environment variable to add the static linking
feature to the target’s compiler configuration:
std::env::set_var("RUSTFLAGS", "-C target-feature=+crt-static");
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
std::env::set_var("RUSTFLAGS", "");
assert!(cfg.target().features().contains(&"crt-static"));Using the -C/--codegen command line option to add the static linking
feature to the target’s compiler configuration:
let cfg = CargoRustcPrintCfg::default()
.rustc_target("x86_64-pc-windows-msvc")
.rustc_args(&["-C", "target-feature=+crt-static"])
.execute()?;
assert!(cfg.target().features().contains(&"crt-static"));Sourcepub fn os(&self) -> &str
pub fn os(&self) -> &str
The target’s operating system.
This is the target_os line in the output. See the
[target_os] section on Conditional Compilation in the Rust
Reference book for example values. The surrounding double quotes, ",
of the raw output of the cargo rustc -- --print cfg command are
removed.
§Examples
For a Windows target:
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
assert_eq!(cfg.target().os(), "windows");For a Linux target:
let cfg = Cfg::rustc_target("x86_64-unknown-linux-gnu")?;
assert_eq!(cfg.target().os(), "linux");For an Apple target:
let cfg = Cfg::rustc_target("x86_64-apple-darwin")?;
assert_eq!(cfg.target().os(), "macos");Note, the target’s OS is different from the target’s family for Apple targets.
Sourcepub fn pointer_width(&self) -> &str
pub fn pointer_width(&self) -> &str
The target’s pointer width in bits, but as string.
This is the target_pointer_width line in the output. See the
target_pointer_width section on Conditional Compilation in the
Rust Reference book for example values. The surrounding double quotes,
", of the raw output of the cargo rustc -- --print cfg command are
removed.
§Examples
For a 64-bit target:
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
assert_eq!(cfg.target().pointer_width(), "64");For a 32-bit target:
let cfg = Cfg::rustc_target("i686-pc-windows-msvc")?;
assert_eq!(cfg.target().pointer_width(), "32");Sourcepub fn vendor(&self) -> Option<&str>
pub fn vendor(&self) -> Option<&str>
The target’s vendor.
This is the target_vendor line in the output. See the
target_vendor section on Conditional Compilation in the Rust
Reference book for example values. The surrounding double quotes, ",
of the raw output of the cargo rustc -- --print cfg command are
removed.
This will return None if the target_vendor line is missing or the
value is the empty string, "".
§Examples
For a Windows target:
let cfg = Cfg::rustc_target("x86_64-pc-windows-msvc")?;
assert_eq!(cfg.target().vendor(), Some("pc"));For a Linux target:
let cfg = Cfg::rustc_target("x86_64-unknown-linux-gnu")?;
assert_eq!(cfg.target().vendor(), Some("unknown"));For an Apple target:
let cfg = Cfg::rustc_target("x86_64-apple-darwin")?;
assert_eq!(cfg.target().vendor(), Some("apple"));