How can one detect the OS type using Rust? I need to specify a default path specific to the OS. Should one use conditional compilation?
For example:
#[cfg(target_os = "macos")]
static DEFAULT_PATH: &str = "path2";
#[cfg(target_os = "linux")]
static DEFAULT_PATH: &str = "path0";
#[cfg(target_os = "windows")]
static DEFAULT_PATH: &str = "path1";
EDIT:
Since writing this answer, it seems the author of the
os_type
crate has retracted functionality that exposed OSes like Windows. Conditional compilation is probably your best bet here --os_type
only seems to detect Linux distributions now, judging from its lib.rs.ORIGINAL ANSWER:
You could always use the
os_type
crate. From the front page:You can also use
cfg!
syntax extension.