Background
MIR stands for Mid-Level-IR and is an Intermediate representation that sits between Rust HIR and LLVM IR. An excellent source for learning more about MIR is Introducing MIR.
I am a systems programmer whose prior systems languages are C and C++. As a systems programmer I am always curious to understand the cost of things and to have some idea of how they are implemented internally. Recently I have been learning Rust and have been looking to bootstrap my understanding of Rust semantics. One technique that worked well for me is to look at the MIR emitted by Rust for small snippets of code and try to understand what is going in. Given how readable and explicit MIR is, I found this approach a much faster way of piercing through syntax and implementation to the underlying semantics. Will share some examples that illustrate this process.
Empty function
|
|
translates to
|
|
Identity function
|
|
translates to
|
|
Variable declaration
|
|
expands to
|
|
Multiple variables
|
|
expands to
|
|
Copying
|
|
expands to
|
|
Immutable borrowing
|
|
expands to
|
|
Mutable borrowing
|
|
expands to
|
|
Mutation via mutable borrow
|
|
expands to
|
|
Explicit drop
|
|
expands to
|
|