Conditionally compiling dependencies based on rust version


I'm stuck with Rust 1.48 at work. But I'd like to be able to make custom builds of the project with some specific memory and tracing instrumentation. This requires pulling some crates that have a MSRV > 1.48.

Let say one such dependency is foo. I'm using a single type foo::Foo from that crate. I could create a dummy foo crate locally, which exposes a Foo type as well. When compiling with 1.48, the project would use the dummy crate. When compiling with > 1.48 it would use the real foo crate.

Is this achievable somehow?

Not really.

Cargo doesn't have a built-in way to detect Rust version. You can do DIY detection in, but that runs too late to affect its dependencies.

You could do it based on a feature flag. Add the breaking dependency as optional, and depending on a feature flag either re-export the dependency (pub use dependency::*) or its dummy replacement.

BTW: it's unusual to use such an outdated Rust version, and the Rust project doesn't support anything older than the latest stable. You will have hard time using crates ecosystem with a compiler that doesn't even support the current Rust edition. You really really should upgrade instead.

1 Like

I really wish I could upgrade, but this is not my call to make :frowning: The pain is getting worse after each release.