Understand your program by having a mental picture
Who said programming should be boring? In this edition, we will bring back the good old times with retro gaming in Rust! Moreover, this newsletter showcases a visualization tool for a better understanding of Rust programs in terms of borrowing, heap, and stack. Grab some coffee and let’s dive in…
Nintendo Emulator in Rust
When you run an NES emulator, written in Rust, on your computer or other devices, it creates a virtual environment that mimics the hardware of the original NES console. The emulator includes software that emulates the various components of the console, such as the CPU, and the graphics processing unit. The emulator also includes software that can read and interpret the code of NES game cartridges, which you can download here and play in the NES wasm emulator in your browser.
Once the emulator is up and running, you can load the virtual game cartridges into the emulator, and it will execute the code of the game as if it were being played on the original console. The emulator will output the video that the game would generate on the original hardware, allowing you to play the game on your computer.
Overall, an NES emulator is a powerful tool that allows users to enjoy classic NES games on modern hardware, preserving the experience of playing these games for future generations.
Memory management: what the hack is a Stack and Heap?
The stack is a memory region that is used to keep track of function calls and local variables. It works like a stack of plates: you add a new plate (or item) to the top of the stack, and when you are done with it, you remove it from the top. This order is called Last In First Out (LIFO). The simplicity makes it fast because it can be managed easily and either lives in RAM (fast) or cache (faster) of your CPU. Moreover, the stack is limited in size
The heap, on the other hand, is a larger and more flexible area of memory that can be used to store data that is too large to fit on the stack or needs to be dynamically allocated during the program’s execution (e.g. mutable vectors or Boxed variables).
Overall, the stack is faster and more efficient, but it has less memory available and is less flexible. The heap is slower and more complex, but it can be used to store larger amounts of data and is more flexible. Knowing when to use each of these memory regions is an important part of writing efficient and effective computer programs. To understand the choices that are made on memory management in Rust (e.g. ownership), knowing what a Stack and Heap are is a must.
Some nice reading material:
- A stack and heap discussion on StackOverflow
- The Stack and the Heap in Rust
- Stack based memory allocation on Wikipedia
- Heap based memory allocation on Wikipedia
Understanding code with help of a visualization tool
To get a better understanding of this topic Aquascope can be a very useful tool.
“Aquascope is a tool that generates interactive visualizations of Rust programs. These visualizations show how Rust’s borrow checker “thinks” about a program, and how a Rust program actually executes.”
Go ahead and give it a try!
Enjoy your Sunday, and have a great week ahead.
Thanks for reading!
Bob Peters
Feel free to connect with me on LinkedIn