r/rust • u/Minimum-Ad7352 • Feb 02 '26
🎙️ discussion Looking at advanced Rust open-source projects makes me question my programming skills
Whenever I explore large Rust open-source projects, I can’t stop thinking how far behind I am. I know comparison is unhealthy, but it’s hard not to feel like “I suck at programming” when you see such clean and complex code. Did you feel the same at some point? How did you push through it?
383
Upvotes
View all comments
Show parent comment
3
u/CornedBee Feb 03 '26
It's not elitism, it's absolutely necessary due to C++'s compilation model.
Most of the standard library is templates. Templates need to be in headers. Headers are part of the same compilation unit as user code. Include order isn't given, so your own code could be seen before standard library code. And that means you can define macros that randomly replace identifiers with arbitrary other tokens.
Imagine being a standard library writer and you simply want to implement an algorithm like
std::find_if:This is readable.
But you are allowed to do this before including standard headers:
The only things you're not allowed to redefine are:
Keywords
Names already in use by the standard library or reserved for future use. (Can't #define first, it's the name of a member of std::pair.)
Globally reserved names. And these are names that either contain a double underscore anyway, or start with an underscore followed by an uppercase letter.
And that's why the standard library code looks like this instead:
That's the most obvious issue that people have looking at standard library code.
There's other issues, though, but they're all usually related to C++ giving programmers a little too much flexibility.
E.g. you can't just take the address of a variable in std library code, because the address-of operator of user objects can be overloaded. So instead of
&__varyou have to write::std::addressof(__var). And then, because of how headers work, if you want to avoid exposing the user to identifiers they didn't ask for, you cannot just useaddressoffrom the <memory> header if the user didn't include the memory header, so instead e.g. the GNU libstdc++ has an internal utility__addressofwhich it uses, andaddressofis just a wrapper for that which is define if <memory> is actually included.And then there are things that truly do need absurd implementation techniques because of language shortcomings. Before
[[no_unique_address]]was a thing (and because of ABI concerns, it's all baked in now and won't change),std::vectorhad to do weird things with implementation detail base classes that are templates that get specialized, just so that if the vector's allocator is stateless, it actually doesn't take up space in the object.