There is example on Conditional compilation - The Rust Reference
#[cfg_attr(target_os = "linux", path = "linux.rs")]
#[cfg_attr(windows, path = "windows.rs")]
mod os;
From this code, I can infer that it is logic OR check on #cfg statement, am I right?
But I can not find clear statement on this check logic on multiple-line #cfg. Better to explicitly state out in the document.
cfg_attr
applies attributes based on the condition in the first argument. Those two attributes are completely independent.
If you use multiple plain cfg
attributes they effectively AND together based on the definition on that page, but that has nothing to do with that example
I know cfg_attr() and cfg when there is only one of them. What is am puzzle is the result of multiple line cfg.
I am trying to grasp you saying. My understand is that, in the example
#[cfg_attr(target_os = "linux", path = "linux.rs")].
#[cfg_attr(windows, path = "windows.rs")]
mod os;
On window, it will resolve to
// empty content after `#[cfg_attr(target_os = "linux", path = "linux.rs")]` be resolved, right ? -- (1)
#[cfg(path = "windows.rs")]
mod os;
On linux, it will resolve to :
#[cfg(path = "linux.rs")]
// empty content after `#[cfg_attr(windows, path = "windows.rs")]` be resolved, right ? -- (2)
mod os;
If it work such way, then developer MUST ensure each #cfg_attr resolved result are mutual exclude in order to get only one #cfg after resolved?
Not quite, it resolves as
#[path = "windows.rs"]
mod os;
or
#[path = "linux.rs"]
mod os;
respectively.
2 Likes