ABSTRACT

This chapter covers references, and borrowing and slice type. It explains about ownership by working through several examples that focus on a fairly popular data structure: strings. The chapter discusses parts of ownership in connection to the stack and heap. The stack and heap are two alternative memory structures that our programs may utilize at runtime. Ownership addresses difficulties such as keeping track of which portions of code use which data on the heap, minimizing the amount of duplicate data on the heap, and cleaning away unnecessary data on the heap, so code don't run out of space. In pointer-based languages, it's possible to construct a dangling pointer, which refers to a place in memory that may have been passed to someone else, by releasing some memory while retaining a pointer to that region. In contrast, the compiler in Rust ensures that references are never dangling. Slices can access data stored in contiguous memory blocks in small blocks.