-
Box
- Allocate data on the heap
- Have fixed size at compile
- Implement recursive types
- Own a type with a specific trait impl
-
Rc
:- Allows multiple owners
- Immutable references for reading only
-
Has
strong_countandweak_count-
Create
Weak<T>instance by callingRc::downgrade -
Use
Rc::upgradeto get backOption<Rc<T>> - Used to prevent reference cycles.
- A child node should not own its parent node: if we drop a child node, the parent should still exist. This is a case for weak references!
-
Create
Interior Mutability:
-
RefCell
:- Single ownership
- Single threaded
- Enforces the borrowing rules invariant at runtime instead of at compile time
-
Allows immutable and mutable borrows via interior mutability.
-
borrow()-> Ref : Deref -
borrow_mut()-> RefMut : Deref
-
-
A common way to use RefCell
is in combination with Rc .
+Send: - Can transfer ownership of values across threads. - Every type impl’s Send expect for RcSend types is automatically marked as Send
-
ARC: Thread-safe version of Rc
-
Also has Arc::downgrade and Arc::upgrade for to/fro Weak
-
Also has Arc::downgrade and Arc::upgrade for to/fro Weak
+Sync: - Safe to be referenced across/from multiple threads. - T is sync if &T is Send - Similar to Send, primitive types are Sync - Rc
-
Mutex: Thread-safe version of RefCell
- AtomicBool
Async:
-
Pin
:- Pins data to its location in memory.