-
Book Overview & Buying
-
Table Of Contents
The Rust Programming Handbook
By :
Borrowing is one of Rust’s most powerful and unique features, enabling data management that is both safe and highly efficient. While Rust’s ownership model guarantees each value has a single owner, there are many cases where you need a function or another part of your program to temporarily access data without permanently taking ownership. This is where borrowing becomes crucial. It allows you to provide access to data without the performance cost of copying it, which is especially important for large data structures such as vectors or strings.
When you borrow a value, you create a reference to it. A reference, indicated by the & symbol, is a type that acts like a pointer, storing the memory address where the data resides, but with a crucial difference: it is governed by Rust’s strict compile-time borrow checker. This means that while a reference allows you to access data without taking full responsibility for it, the compiler...