cfg
Configuration conditional checks are possible through the cfg attribute #[cfg(...)]
This attribute enables conditional compilation, removing code that is not valid for the current configuration.
For example, a package supporting various hash functions might define features like this in the
Scarb.toml file:
[package]
name = "cfg"
version = "0.1.0"
edition = "2024_07"
# See more keys and their definitions at https://docs.swmansion.com/scarb/docs/reference/manifest.html
[dependencies]
[dev-dependencies]
cairo_test = "2.9.2"
[features]
default = ["poseidon"]
poseidon = []
pedersen = []
And then use the features in the source code:
// This function only gets compiled if the feature "poseidon" is enabled
#[cfg(feature: "poseidon")]
fn which_hash_function() {
println!("You are hashing with Poseidon!");
}
// This function only gets compiled if the feature "poseidon" is not enabled
#[cfg(not(feature: "poseidon"))]
fn which_hash_function() {
println!("You are **not** hashing with Poseidon!");
}
fn main() {
which_hash_function();
}