Borrowing

Borrowing is the concept behind Rusts references. A reference is a value that refers to a different value, without owning it; it only borrows the value.

Example
The  function accepts a value of type , which is a   behind a reference. It allows the  function to access the string, without owning it, so when   goes out of scope in line 3, the underlying string is not dropped and can still be used in line 8.

The Borrow Checker
Here's an example that does not work: This produces the following error message: We tried to return a reference to a variable. However,  goes out of scope at the end of the function and is dropped. If we returned a reference to, it would point to an invalid memory location. Luckily Rust prevents us from doing this: In Rust, every reference is assigned a lifetime, and the compiler makes sure that no reference lives longer than the value it points to. The part of the compiler that validates all the lifetimes is called the borrow checker.