Skip to content

Commit 2b9bb3b

Browse files
committed
docs: Add new Rust documentation and journal entries
- Created a new journal entry for December 9, 2025, linking to the concept of Box in Rust. - Added a new page detailing the ownership concept in Rust, including examples and explanations. - Introduced a page for Rust Box, explaining its role as a pointer to heap memory. - Updated the main Rust page to include a reference to the new documentation.
1 parent c557edc commit 2b9bb3b

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

journals/2025_12_09.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- [[Rust/Box]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- See [[Rust]]

pages/Rust.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
alias:: [[Programming/Language/General Purpose/Rust]]
1+
- Rust
2+
-
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
- [What is Ownership? - The Rust Programming Language](https://rust-book.cs.brown.edu/ch04-01-what-is-ownership.html)
2+
- [[Rust/Variable]]s by live in the scope of function's stack, and are deallocated at the end of the function.
3+
- [[Rust/Box]]es, in contrast, live in the heap.
4+
- Internally, [[Rust/Collection]]s like [[Rust/Vec]] and [[Rust/String]] use `Box`es.
5+
- Rust is unique because it does not permit manual memory management.
6+
- [[Rust/Variable]]s cannot be used after being moved.
7+
- The following does not compile:
8+
- ```rust
9+
fn main() {
10+
let first = String::from("Ferris");
11+
let full = add_suffix(first);
12+
// does not compile, first has been moved!
13+
println!("{full}, originally {first}");
14+
}
15+
```
16+
- To get around this, you can clone the string so there are two copies.
17+
- [[Rust/Ownership]] comes into play in the difference between these two phrases:
18+
- **Box deallocation principle (almost correct):** if a variable **is bound to** a `Box`, when rust deallocates the variable's frame, then Rust deallocates the box's heap memory.
19+
- **Box deallocation principle (fully correct):** if a `Box` **is owned by** a variable, when Rust deallocates the variable's frame, then Rust deallocates the Box's heap's memory.
20+
- Here ownership refer s to how variables can fall out of scope.
21+
- None of the following example compile;
22+
- ```rust
23+
// example 1
24+
let b = Box::new(0);
25+
let b2 = b; // this is okay
26+
move_a_box(b); // not okay: Box is not owned by b
27+
28+
// example 2
29+
let b = Box::new(0);
30+
move_a_box(b); // Box passed to func. when done, rust deallocates its mem
31+
println!("{}", b); // Box is not owned by b
32+
33+
// example 3
34+
let b = Box::new(0);
35+
move_a_box(b); // Box passed to func. when done, rust deallocates its mem
36+
let b2 = b; // box is not owned by b
37+
```
38+
-

pages/Rust___Box.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- [[Rust/Book/TRPL/ch/04/01 What is Ownership]]
2+
- A box is a special kind of pointer to memory in the heap.
3+
- A variable that is a Box like `let b = Box(15)` contains a pointer to memory, so its ownership can be transferred.
4+
-

0 commit comments

Comments
 (0)