How do I conditionally compile with new Rust features while still supporting older Rust versions?

I want to use min_const_generics in a library, but fall back to a different implemention for users of Rust 1.50 and below, where it isn't available.

e.g.

#[cfg(min_const_generics)]
mod foo {}

#[cfg(not(min_const_generics))]
mod foo {}

This doesn't work, and the only methods I've come up with are:

  1. use explicit crate features to opt in or out of min_const_generics.
  2. publish a new version that uses min_const_generics but also maintain an older version that doesn' use it.

Am I missing something or is there really no way to express this?

As is often the way, almost immediately after writing this post I found https://github.com/dtolnay/rustversion

I assume that crate wouldn't exist if there was a built-in way to do it.

3 Likes

I would just use a build script. I think appropriately named cfgs are more readable than specifying rust versions everywhere. For example see semver/build.rs at master · dtolnay/semver · GitHub

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.