How are the generic functions and types stored in an rlib?

1k views Asked by At

In C++, the templates can not be generated into dynamic libraries, we can only use them by header files.

In C#, generic functions and types can be interpreted by intermediate language in .NET.

Rust has no virtual machine, and the generics can be stored in the rlib files. How does it managed to achieve this? What is the format of rlib files?

1

There are 1 answers

0
DK. On BEST ANSWER

An rlib is a regular static library (built in the ar format) that contains additional metadata. That metadata contains, among other things, the complete, serialised abstract syntax tree (AST) for all generics and functions marked with #[inline].

It's a bit like if there was a C++ compiler that shoved a library's header files into the compiled binary, and then read them out again when linking against that library.