Using approx crate via a feature in tests

I cannot seem to figure out how to properly enable a feature flag in order to use methods / traits which are injected into ndarray when that feature is enabled.

I keep getting this error:

assert!(expected.abs_diff_eq(&clip.slice(s![..;350, ..])));
    |                          ^^^^^^^^^^^ method not found in `ndarray::ArrayBase<OwnedRepr<{float}>, Dim<[usize; 2]>>`

I tried to model my Cargo.toml after the ndarray crate with respect to tests and approx.

approx = { version = "0.4", optional = true , default-features = false }


test = ["approx"]

I've added a cfg declaration above the test that uses this as well.


I have tried passing --features approx to cargo but that doesn't help either.

The ndarray code that adds these approx based methods is in ndarray/ and the snippet below:

#[cfg(feature = "approx")]
mod array_approx;

The [features] section is (primarily) for declaring features of the current crate, not enabling/disabling features of dependencies. You probably want this:

# other dependencies

approx = { version = "0.4", features = ["approx"] }

Thanks! That was the hint I needed. Makes sense that I am turning it on in the dependency build rather than the current crate build.

It was actually the ndarray dependency that needed it passed in:

ndarray= {version="0.14", features=["approx"]}

I kept put this in dev-dependencies and have a separate ndarray requirement without this feature flag in the main dependencies (which I just left alone), does this mean I build ndarray twice, one for tests and the other for main builds?

Oops, yes, approx there was a typo on my part.

No, it will be compiled once with the union of features specified under [dependencies] and [dev-dependencies]. (This is not really ideal behavior and is one of the things that will change when Cargo's "features 2.0" rolls out.)

Got it, makes sense! Thanks for the help!

1 Like

For reference, “features 2.0” is on track to ship next month in Rust 1.51. You’ll need to add resolver = "2" in Cargo.toml to take advantage of it: Dependency Resolution - The Cargo Book

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.