Skip to content

Commit f5b0c44

Browse files
committed
docs: Add new journal entries and Rust documentation
- Created a journal entry for December 12, 2025, documenting insights on Rust study topics including references, borrowing, and ownership errors. - Added detailed pages on Rust async programming and closures, explaining key concepts and syntax. - Included links to relevant chapters from "The Rust Programming Language" for further reading.
1 parent 59b76cc commit f5b0c44

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

journals/2025_12_12.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- [[Rust]] study
2+
- [[Rust/Book/TRPL/ch/04/02 References and Borrowing]]
3+
- I'm now starting to understand why for a perfectionist's language, Rust code tends to be less [[DRY]] than some other languages like [[Haskell]].
4+
- [[Rust/Book/TRPL/ch/04/03 Fixing Ownership Errors]]
5+
- [[Langstar]] development
6+
- This morning I got in early and had two or three tries at using [[Git Worktrees]] to do work with [[Claude Code]]. Even though I've been relatively successful lately using a [[DevContainer]] to do 3-5 trees, I'm finding that docker and the devcontainer crash more and more when I'm doing unattended coding on Rust. I think it might have to do with rust extensions doing type checking in the IDE on the various worktrees while the source code is changing, and Docker bumping into its memory limit. But I'm a bit surprised that 16 GB of [[RAM]] wasn't enough. It could also have to do with switching to [[Cargo/nexttest]], which has a higher CPU burden. With one worktree active in claude code, I'm at 5.34 GB of container memory usage. Within 10 min it was up to 10GB.
7+
- [[Rust/Closure]]
8+
- [[Rust/async]]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [References and Borrowing - The Rust Programming Language](https://rust-book.cs.brown.edu/ch04-02-references-and-borrowing.html)
2+
-
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- [Fixing Ownership Errors - The Rust Programming Language](https://rust-book.cs.brown.edu/ch04-03-fixing-ownership-errors.html)
2+
-

pages/Rust___Closure.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Rust Closure - `| x |`
2+
- via [[Langstar/PR]] [🧪 test(project): add comprehensive tests for project commands by codekiln · Pull Request #709 · codekiln/langstar](https://github.com/codekiln/langstar/pull/709/changes)
3+
- ## Basic Idea
4+
- In this code:
5+
- ```rust
6+
let found = projects.iter().any(|p| {
7+
p.get("id")
8+
.and_then(|v| v.as_str())
9+
.map(|id| id == project_id.to_string())
10+
.unwrap_or(false)
11+
});
12+
```
13+
- `|v|` is just the parameter list for a **closure** in Rust.
14+
- ### What this closure is
15+
- `and_then` takes a function that receives the inner value of an `Option`.
16+
In this case, `p.get("id")` returns an `Option<&Value>` (`serde_json::Value`).
17+
- So `and_then(|v| v.as_str())` means:
18+
- If the option is `Some(v)`, call `v.as_str()`.
19+
- If the option is `None`, skip the closure and return `None`.
20+
- ### What `|v|` means specifically
21+
- `| ... |` is the syntax for closure parameters.
22+
Inside the bars goes the argument list:
23+
- `|v|` → one argument named `v`
24+
- `|a, b|` → two arguments
25+
- `||` → no arguments
26+
- The body follows after the `|...|`.
27+
- ### Full semantics
28+
- `|v| v.as_str()` is roughly equivalent to writing:
29+
- ```rust
30+
fn temp(v: &serde_json::Value) -> Option<&str> {
31+
v.as_str()
32+
}
33+
```
34+
- …but inline and anonymous.
35+
- ### Summary
36+
- `|v|` is simply the closure argument list.
37+
The whole expression applies a closure to the `Some(v)` branch of the `Option`, transforming `Option<&Value>` into `Option<&str>` by calling `.as_str()` on the JSON value.
38+
- ## What if `p.get("id")` is `false`?
39+
- In the selected block:
40+
- ```rust
41+
projects.iter().any(|p| {
42+
p.get("id")
43+
.and_then(|v| v.as_str())
44+
.map(|id| id == project_id.to_string())
45+
.unwrap_or(false)
46+
})
47+
```
48+
- If `p.get("id")` is `None`, here is the exact flow:
49+
- `p.get("id")` → `None`
50+
- `None.and_then(|v| v.as_str())` → still `None`
51+
- `None.map(|id| ...)` → still `None`
52+
- `None.unwrap_or(false)` → returns **false**
53+
- So the closure returns **false**, meaning that this `p` does *not* match.
54+
- ### Why this works
55+
- It’s using the `Option` combinators as a pipeline:
56+
- `get("id")` fetches the JSON field (or not).
57+
- `and_then` ensures the value is actually a string.
58+
- `map` performs the equality check if possible.
59+
- `unwrap_or(false)` means: *if anything in the chain failed, treat as no match*.
60+
- The `any()` call only returns `true` if at least one project has an `"id"` that matches.

pages/Rust___async.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
- # Rust async
2+
- Here's a clean, Rust-native explanation of the selected line and the constructs involved.
3+
- ## 🔍 The selected line
4+
- ~~~rust
5+
let _cleanup = runtime.block_on(async { client.delete_project(project_id).await });
6+
~~~
7+
- This combines **four** concepts:
8+
- **`async { ... }` block**
9+
- **`.await` operator**
10+
- **A future value**
11+
- **A synchronous executor call (`block_on`)**
12+
- Let's break those down.
13+
- ## 1. `async { ... }` — an async block expression
14+
- `async { ... }` is an **expression** that returns a *future* — specifically an *anonymous type that implements `Future<Output = T>`*.
15+
- Inside an async block, you can use `await`, but the block itself **does not run** until something *polls* the future (usually an executor).
16+
- It is similar to:
17+
- ~~~rust
18+
async fn foo() -> T { ... }
19+
~~~
20+
- but inline.
21+
- Other Rust constructs that work the same way (i.e., block forms that produce values) include:
22+
- `unsafe { ... }`
23+
- `const { ... }`
24+
- `try { ... }` (unstable; returns `Result`)
25+
- `loop { ... }`, `if { ... }`, etc., because *blocks are expressions*
26+
- Rust's rule: **Blocks are expressions that evaluate to the last expression inside.**
27+
- So the async block:
28+
- ~~~rust
29+
async {
30+
client.delete_project(project_id).await
31+
}
32+
~~~
33+
- produces a future whose output type is whatever `.await` returns — here likely `Result<(), SomeError>` or whatever the API defines.
34+
- ## 2. What is `.await`?
35+
- `.await` is **neither a method nor a property** — it is a **postfix operator built into the language parser**, like `?`.
36+
- It transforms a `Future<Output = T>` into a `T`, *but only inside an async context*.
37+
- If you want an analogy:
38+
- It is like Python `await f()`, **but syntactically resembles a field access** (`obj.field`).
39+
- Under the hood, it compiles down to state-machine code that polls the future and suspends.
40+
- Its "type"?
41+
- The expression:
42+
- ~~~rust
43+
some_future.await
44+
~~~
45+
- has the type **`T`**, where the original future was `Future<Output = T>`.
46+
- ## 3. The sequence of evaluation
47+
- Your full line:
48+
- ~~~rust
49+
let _cleanup = runtime.block_on(async { client.delete_project(project_id).await });
50+
~~~
51+
- ⚙️ *Steps:*
52+
- `async { ... }` creates a future of type `impl Future<Output = DeleteResult>`.
53+
- `runtime.block_on(...)` takes that future and **runs it to completion**, turning async → sync.
54+
- The result of the `.await` inside is returned out of the async block.
55+
- That value becomes `_cleanup`.
56+
- ## 4. Are there other parts of Rust with similar syntax?
57+
- Yes — Rust has several "block operators" and "postfix operators":
58+
- ### **Block expressions:**
59+
- `async { … }` — future-producing
60+
- `unsafe { … }` — unsafe block
61+
- `try { … }` — result-producing block (unstable)
62+
- `const { … }` — const-evaluated block (stable)
63+
- `{ … }` plain blocks
64+
- ### **Postfix operators (built-in, not methods):**
65+
- `.await`
66+
- `?`
67+
- Array indexing (`arr[idx]`)
68+
- Deref and reference are prefix/suffix but part of expression syntax, not methods.
69+
- These are all *language-level constructs*, unlike Python where most constructs desugar into method calls.
70+
- ## Quick mental model
71+
- **`async { ... }`** → "Create a future value."
72+
- **`.await`** → "Extract the future's output; pause until ready."
73+
- **`block_on(fut)`** → "Drive the future to completion from sync code."
74+
- You can think of `.await` like Python `await`, **but it is not a function or property — it is language syntax.**

0 commit comments

Comments
 (0)