Why you shouldn't obsess about Rust "features"

submited by
Style Pass
2021-07-07 16:30:05

Rust makes it very easy to express conditional compilation, specially thanks to its “features”. They’re well integrated into the language and are very easy to use. But one thing I’ve learned by maintaining Rspotify (a library for the Spotify API) is that one shouldn’t obsess over them. Conditional compilation should be used when it’s the only way to solve the problem, for a number of reasons I’ll explain in this article.

This might be obvious to some, but to me it wasn’t so clear back when I started using Rust. And even if you’re already aware, it might be an interesting reminder; maybe you forgot about it in your latest project and added an unnecessary feature.

Conditional compilation isn’t anything new either; C and C++ have been doing it for a long time now, for one. So the same thing can be applied in these cases. However, in my experience it’s much easier to work with conditional compilation in Rust, meaning that it’s more likely to be misused as well.

I went through this dilemma when deciding how to configure cached tokens in Rspotify. Said library gives you the possibility of persistently managing the authentication token via a JSON file. That way, when the program is launched again the token from the previous session can be reused without having to follow the full auth process again — that is, until the token expires.

Leave a Comment
Related Posts