References and Borrowing
202307032153
Status: #idea
Tags: Rust
References and Borrowing
- A reference is a non-owning pointer
Caution
References are immutable by default
Dereferencing a Pointer
- Dereferencing a pointer, accesses its data
Code example with comments: Pointers
Implicit Dereferencing
- Rust will implicitly dereference a pointer based on the data type of the variable
Code example with comments: Implicit dereference
Simultaneous Aliasing & Mutation
Summary
- All variables read, own and (optionally) write their data
- Creating a reference transfers permissions to the reference
- Permissions are returned once the reference's lifetime has ended
- Data must outlive all references that point to it