Approaches to memory allocation
At runtime, memory allocations in a process happens either on the stack or on the heap. They are storage locations that are used to store values during the execution of the program. In this section, we'll take a look at both of these allocation approaches.
The stack is used for short-lived values whose sizes are known as compile time, and is the ideal storage location for function calls and their associated context, which needs to go away once the function returns. The heap is for anything that needs to live beyond function calls. As mentioned in Chapter 1, Getting Started with Rust, Rust prefers stack allocation by default. Any value or instance of a type that you create and bind to a variable gets stored on the stack by default. Storing on the heap is explicit and is done by using smart pointer types, which are explained later in this chapter.
The stack
Any time we call a function or a method, the stack is used for allocating space for values that are created...