Title: Theory / Unimportant / CS / Programming / Rust / Library / Pointer / Box
Tags: computer_to_instruction

Question: When to use Box<T>?
Answer: When you have a type whose size can’t be known at compile time and you want to use a value of that type in a context that requires an exact size
Page: https://doc.rust-lang.org/book/ch15-01-box.html
Chapter: 15
Context: Rust
Answer2: When you have a large amount of data and you want to transfer ownership but ensure the data won’t be copied when you do so
Answer3: When you want to own a value and you care only that it’s a type that implements a particular trait rather than being of a specific type

Instruction: Box::new(v)
Page: https://doc.rust-lang.org/book/ch15-01-box.html
Effect: Put v in heap
Language: Rust
Chapter: 15
Returns: A pointer to v that gets deallocated when the value gets out of scope
Typ: Box<T>

Typ: Box
Meaning: A pointer type for heap allocation.
Function: fn deref(&self) -> Target
Page: https://doc.rust-lang.org/book/ch15-01-box.html
Name: Deref
Context: Rust
Chapter: 15

Instruction: Box::pin(x)
Page: https://doc.rust-lang.org/std/boxed/struct.Box.html#
Language: rust
Returns: A Pin<Box<T>> containing x
Variables: x: T

Instruction: Box::from_raw(raw_pointer)
Page: https://doc.rust-lang.org/std/boxed/struct.Box.html#
Language: rust
Returns: A box containing same content as raw pointer.
Where to use it: unsafe
Assuming: raw pointer point to a value following rust memory layout. E.g. allowcated with into_raw

Instruction: Box::into_raw(box)
Page: https://doc.rust-lang.org/std/boxed/struct.Box.html#
Effect: consume the box
Language: rust
Returns: A raw pointer to the content of the box.
Where to use it: unsafe
Intuition: Ensure that box don't deallocate content, allow us to deal with lifetime manually

Instruction: box.downcast::<type>()
Page: https://doc.rust-lang.org/std/boxed/struct.Box.html#
Language: rust
Returns: Result with a box of type type if possible and original box in case of error
Variables: box: Box<T>
Where to use it: unsafe

Instruction: box.as_mut()
box.borrow_mut()
Page: https://doc.rust-lang.org/std/boxed/struct.Box.html
Returns: &mut reference to box content
Variables: boxed: Box<T>

Instruction: box.as_ref()
box.borrow()
Page: https://doc.rust-lang.org/std/boxed/struct.Box.html
Returns: const reference to box content
Variables: boxed: Box<T>