The Rust language concept hierarchy

  • The bottom layer of Rust is the secure memory management layer, and the cover layer is mainly designed with the concepts related to memory management
  • The type system layer is a layer connecting the preceding and the following. The type system carries all the system-wide semantics and mixed programming paradigms from the upper layer, which gives Rust high-level abstract expressiveness and security. It also retains control over operations such as underlying code execution, data representation, and memory allocation.

For the average developer, you only need to master the type system, ownership system, and hybrid programming paradigm, and you don’t need to worry about whether the underlying memory is safe, because the compiler and type system take care of it. People and compilers share the same set of “mental models”, which greatly ensures the security and robustness of the system.

How does Rust code execute

Rust is a cross-platform language that can be compiled and exported once, thanks to LLVM. Rust’s compiler is a front end to LLVM compilation, which translates code as LLVM IR and then compiles it to the appropriate platform target.

  • Rust generates an AST (abstract syntax tree) through word segmentation and parsing.
  • Then the AST is further simplified to HIR(Hight-level IR), in order to make the compiler more convenient to do type checking.
  • HIR is further interpreted as MIR (middle-level IR), an intermediate representation introduced in Rust 1.12, primarily for use
    • Shorten compilation time. MIR can help with incremental compilation, so that when you recompile the code after you modify it, the compiler only calculates the changes, reducing the compile time
    • Shorten the execution time. MIR allows for finer grained optimization before LLVM compilation, because the granularity of optimization relying on LLVM alone is too coarse and Rust cannot control it, and the introduction of MIR adds more room for optimization.
    • More accurate type checking. MIR will help implement more flexible borrowing checks, which can improve the Rust experience.
  • Eventually MIR is translated into LLVM-IR, which is then processed by LLVM as the target machine code to run on each platform