How to/where am I to activate cfg value

I have in code something like:

#[cfg(fs_extra)]

Where in the crate I am supposed to say:

fs_extra = true

Thanks

Cfg values are passed to rustc when you invoke it. For example rustc --cfg fs_extra src/main.rs. In case you use cargo, you should use #[cfg(feature = "fs_extra")] instead and add the following to Cargo.toml:

[features]
fs_extra = []

You can then enable it when building using cargo build --features fs_extra. Or in case of a dependency you specify it using something like my_dep = { version = "0.1.0", features = ["fs_extra"] }. See Features - The Cargo Book more more information.

4 Likes

In my .rs file I've put:

 #[cfg(feature = "serial_cpy")]
 {
    cpy_task(paths_from, path_to);
 }

In my Cargo.toml I've put:

[features]
serial_cpy = [ ]

Still not working. What am I doing wrong?

Are you building with cargo build --features serial_cpy?

1 Like

You can also make it default if you don't want to pass it yourself every time:

[features]
default = ["fs_extra"] 
fs_extra = [] 

Yes

I did that too. Still no luck. I must be doing something wrong.
My Cargo.toml

[features]
defaults = ["serial_cpy"]
serial_cpy = []

My .rs:

 #[cfg(feature = "serial_cpy")]//I want this to be active only if I specify it in Cargo.toml
                        {
                            cpy_task(paths_from, path_to, counter.clone(),cb);
                        }

The key should be default, not defaults (no S).

That looks correct to me. Have you tried putting eprintln!("test") or something inside the block to check for sure whether it's being compiled?

1 Like

Thanks to everyone for taking the time and helping me.

1 Like