Using newly stabilized library features in new compiler but not in old?

Is there a way to use a newly stabilized library feature when compiling with the latest compiler while still using the old way with older compiler?

I'm running into an issue with std::ptr::fn_addr_eq(a as fn(), b as fn()) (which was stabilized in 1.85) as a replacement for a as fn() == b as fn(). In 1.85 there is also a new clippy lint that recommends the former rather than the latter.

Is there a way to use the new way on compilers with 1.85 or later while still using the old way on previous compilers?

EDIT: I thought I remember reading somewhere about an attribute to address this issue, but I can't recall where or what to search for. Everything I've looked for hasn't been helpful.

There is the rustversion crate that you could use to annotate your items and conditionally compile based on the version of the compiler.

1 Like

In the specific case of fn_addr_eq(), you should just use the old way — or define your own fn_addr_eq() function for specific cases — with an allow for the lint. This is because the purpose of fn_addr_eq() is to document what the code is doing, and a cfg conditional is overall making the code harder to comprehend, not easier.

1 Like

Maybe you're thinking of #[cfg(accessible(path))]? But this isn't stable yet.

1 Like

Yes, that is exactly what I was thinking of. Too bad it isn't stabilized yet.