How to pass a option at build time to decide whether to enable a function

In Rust, is it possible to pass some options at build time, like cargo build -- --plot, to determine whether or not to enable a function? I want the function to be executed only if there is an option like this

#[plot]
plot();

I don't want to pass options at runtime and use if to determine whether to execute a function or not. Are there any solutions?


In Rust, you can use the option of Conditional compilation. It can be conditionally compiled using the attributes cfg and cfg_attr and the built-in cfg macro. It should be noted that these conditions are based on the target architecture of the compiled crate, arbitrary values passed to the compiler, and a few other things as well as described in the link shared below.

The conditional compilation takes a configuration predicate that evaluates to true or false and the predicate can be either of below :

a. A configuration option - true if the option is set and false if it is unset.

b. all() - It is false if at least one predicate is false. If there are no predicates, it is true.

c. any() - It is true if at least one predicate is true. If there are no predicates, it is false.

d. not() - It is true if its predicate is false and false if its predicate is true.

Configuration options are names and key-value pairs that are either set or unset. Names are written as a single identifier such as, for example, unix. Key-value pairs are written as an identifier, =, and then a string.

Reference : https://doc.rust-lang.org/reference/conditional-compilation.html