How to learn rustc source code?

With the help of some information I recently tried to learn the source code of the rust standard library, after a few days found that the obstacles are very large, there are many reasons, one of the more important aspects from the unfamiliarity of the rustc and can not understand some writing method can not find useful information; who has good learning materials in this area (blog or video can be) can tell me?

The question is very vague, it is not at all clear to me what kind of obstacles you are encountering. Maybe you can ask some questions here of concrete things you don’t understand or find confusing, and we can perhaps answer them here on one hand, and give you sources where or approaches how those questions could be answered on your own on the other hand.

For example, when I was looking at the standard library code, I encountered a similar way of writing (#[lang = "pointee_trait"]), but when I encountered a problem I always wanted to immediately understand the principle mechanism and how it was implemented thoroughly, and when I searched the code globally, I located the rustc directory, but even after opening it, I still couldn't understand it, so I thought I would figure out rustc first and then go back to it. The standard library code would be easier, but I found more resistance. Is there something wrong with my learning style?

May I ask what you want to achieve by understanding the source code of the standard library? Do you want to contribute to it, are you just curious, do you want to learn idiomatic rust code or something completely different? I'm asking, because depending on your intentions, what you are trying to do may not be the most viable try. Rustc is probably the most complex program written in rust out there and the standard library is also very much something to be used by the everyday rust user, not something you necessarily need to understand the implementation details of.

Here’s a page about lang items

Lang items are in principle just a way of marking a specific special thing in the standard library in a way so that the rust compiler can find it. For example Iterator::next and IntoIterator::into_iter are lang items not because they are special or “magical” in a way but because they appear in the desugaring of for loop syntax. On the other hand, e.g. Box is a lang item and it has certain special/magical properties, e.g. the ability to dereference them for owned access, including the ability partially move out of them.

Note that the Pointee trait you’re mentioning is also an abstraction that only exists in the standard library as part of an unstable feature, so far. So it’s only of interest in case you are interested in details of an unstable feature (which might very well still change in the future before being stabilized). Why this trait is a lang item is easy to see when you look at its documentation

The point of this trait is its Metadata associated type, which is () or usize or DynMetadata<_> as described above. It is automatically implemented for every type. It can be assumed to be implemented in a generic context, even without a corresponding bound.

So it’s one of the more “magical” lang items, in particular this trait is “automatically implemented for every type” by the compiler, and that can naturally only happen if the compiler has a way of identifying/naming that trait that it’s supposed to automatically implement.

2 Likes

Understanding the source code of the standard library comes from interest on one hand, especially the internal principle mechanism (don't want to hold a lot of questions to write code), on the other hand, you can write a better performance program, and finally if you have the opportunity, you may contribute code to the standard library (although this possibility is currently unlikely).