get system architecture in rust

In Rust, you can use the std::env::consts module to get information about the target architecture.

To get the CPU architecture, you can use the arch function:

main.rs
use std::env;

fn main() {
    let cpu_arch = env::consts::ARCH;
    println!("CPU architecture: {}", cpu_arch);
}
115 chars
7 lines

To get the endianness of the target architecture, you can use the endianness function:

main.rs
use std::env;

fn main() {
    let endianness = env::consts::ARCH;
    println!("Endianness: {}", endianness);
}
113 chars
7 lines

Note that some architectures, such as ARM, support multiple endianness modes. In this case, endianness will return either "little" or "big".

To get the target architecture itself (e.g. "x86_64" or "arm"), you can use the target_arch function:

main.rs
use std::env;

fn main() {
    let target_arch = env::consts::ARCH;
    println!("Target architecture: {}", target_arch);
}
124 chars
7 lines

Similarly, to get the endianness of the target architecture, you can use the target_endian function:

main.rs
use std::env;

fn main() {
    let target_endian = env::consts::ARCH;
    println!("Target endianness: {}", target_endian);
}
126 chars
7 lines

gistlibby LogSnag