This chapter covers:
- discovering what the term “lifetime” means in the context of Rust programming
- working with the borrow checker rather than against it
- multiple tactics for dealing with issues when they crop up
- understanding what the responsibilities of an “owner” are
- learning how to “borrow” values that are owned elsewhere
This chapter attempts to explain one of the concepts that trips up most newcomers to Rust: its “borrow checker”. The borrow checker checks that all access to data is legal. Checking to see that all data access is legal allows Rust to prevent safety issues.
Learning how this system works will—at the very least—speed up your development time, by avoiding run ins with the compiler. More significantly though, learning to work with the borrow checker allows you to build larger software systems with confidence. It underpins the term “fearless concurrency”.
To explain how this system operates—and learn how to comply with it when an error is discovered—is this chapter’s role. It uses the somewhat lofty example of simulating a satellite constellation to explain the trade offs relating to different ways to provide shared access to data.
The details of borrow checking are thoroughly explored within the chapter. However, a few bullet points might be useful for readers wanting a quick gist:
Borrow checking relies on three inter-related concepts: lifetimes, ownership and borrowing.