summaryrefslogtreecommitdiff
path: root/Creating a reference to a variable is called borrowing.md
blob: 2790d6335cd5c9b7d88f1d8723680297f17e1628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
[[Rust]]
[[References are also immutable by default]]
[[Mutable references are safe because they prevent aliasing]]
[[Ownership]]
[[Dereferencing is to access the data behind a pointer]]

---

Creating a reference is done with an ampersand. It creates a pointer to another variable in the stack, which could be a pointer to something on the heap.

This is called borrowing. ( Borrow, but not own, its heap data so to speak? )

A reference is a non-owning pointer; so if we create a reference to a Box we can safely have it discarded and continue using the box because the heap data is not deallocated.