Target

Struct Target 

Source
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

Source

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");
Source

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");
Source

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"));
Source

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"));
Source

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"));
Source

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.

Source

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");
Source

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"));

Trait Implementations§

Source§

impl Clone for Target

Source§

fn clone(&self) -> Target

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Target

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Target

Source§

fn eq(&self, other: &Target) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Target

Auto Trait Implementations§

§

impl Freeze for Target

§

impl RefUnwindSafe for Target

§

impl Send for Target

§

impl Sync for Target

§

impl Unpin for Target

§

impl UnwindSafe for Target

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.