Using nightly-only methods -- is it safe?

I have been experimenting with the nightly-only method remove_matches() that is listed in the std::string::String library. It does what I need, but since it is experimental, is it safe for me to use it?

Safe as in "it won't cause It UB", yes, but bare in mind that these methods are not stable and their implementation might change

1 Like

AFAIK the unstable features which may cause crashes will issue a warning when used, so if you don't get a warning, I think you should be alright.

However, as @moy2010 pointed out, you would need to be ready for the API to change, i.e. your code may fail to compile from one version of Rust to the next, which then requires adjustments from your side.

If you want to avoid this, stick to the stable interfaces.

What I often do in my code is I try to see if the experimental interface would work, and then I revert it to a stable alternative but add a TODO in my code to remind me switching back to the improved API once it stabilizes.

For example:

// TODO: use `get_or_insert_owned` if stabilized
// x.get_or_insert_owned(&y);
if !x.contains(&y) {

You can often get a feeling for how likely an API is to be stabilized as is by following the link on the docs page to the tracking issue. Some APIs are very experimental, while others are in the process of being stabilized.

There's of course no guarantee that changes won't be made even for APIs that are approaching stabilization, but depending on the project that risk may be acceptable.

If you're using nightly APIs it may be a good idea to pin the nightly version you're using for the project with a rust-toolchain.toml file so you can still build the project if the APIs do change. You'll want to update the toolchain periodically of course, but having a pinned version will ensure you at least know what version of the compiler it was previously building with.

Just to share a comment on reddit I saw yesterday about nightly (unstable) features.

Now let me start by saying that I'm by no means an expert but I was at a meetup where one of the "talks" was a conversation with the head of the libs team so I want to add some of the insights I got there.

If something is in nightly it's basically halfway to stable. It has already been proposed, discussed and implemented in nightly so people already put in a lot of work. If the issue died out that means (most of the time) that it's either blocked on some other nightly feature or that the people pushing it along burnt out/found some other solution/found a problem they were unsure how to fix. She said something along the lines of "if there is an unstable feature you want to get to stable, just revive the issue, start discussing what needs to be done to get it to stable and then put in the work". Rust doesn't function like a company, the people in the libs team for example can't just assign work to employees. To make progress on things a motivated individual needs to put in the hours or a company which lets employees work on rust needs to make it their priority.

Update: I found the meetup recording here: Q&A with Mara Bos - March 9 2023 - YouTube :laughing:


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.