Our tale about memory management would be incomplete if we didn't include pointers in the discussion, which are the primary way to manipulate memory in any low level language. Pointers are simply variables that point to memory locations in the process's address space. In Rust, we deal with three kinds of pointers.
These pointers are already familiar to you from the borrowing section. References are like pointers in C, but they are checked for correctness. They can never be null and always point to some data owned by any variable. The data they point to can either be on the stack or on the heap, or the data segment of the binary. They are created using the &
or the &mut
operator. These operators, when prefixed on a type T
, create a reference type that is denoted by &T
for immutable references and &mut T
for mutable references. Let's recap on these again:
&T
: It's an immutable reference to a typeT
. A&T
pointer is aCopy...