Tower Service and Layer — HTTP middleware chains with hyper

Rust之tower如何构建请求中间件

When people talk about Rust HTTP middleware, Tower comes up fast.

Tower is protocol-agnostic middleware plumbing; Service and Layer are how you stack Logger, timeout, and handler layers.

Read More

tokio-console — observe Tokio task scheduling and runtime behavior

tokio之如何观测Rust异步任务的调度

Rust async is built around task scheduling—tasks are the language-level unit the executor moves between.

Can you see that scheduling happen? Yes. Tokio ships tokio-console: a live view of tasks, polls, and wakeups that helps when you’re debugging latency or learning how the runtime behaves.

Read More

Rayon — data parallelism with par_iter and work stealing

Rayon魔法:使Rust并行编程变得轻而易举

Rayon parallelizes CPU-bound iterators: swap in par_iter (and friends), keep order when Rayon documents it, and let the type system reject data races at compile time.

Read More

Tokio — join, spawn, and select for concurrent Futures

Rust异步编程之Future并发处理

In the previous post, a Future runs when you .await it—and that wait blocks the current task until that future completes.

If every async operation had to finish before the next one started, you’d be back to serial code. Here we look at Tokio patterns for real concurrency across multiple futures.

Read More

Rust Future — poll, state machines, Tokio runtime, and Pin

Rust异步编程之Future初探

Rust’s Future is the core abstraction for async programming. What follows is how async/await, polling, and the runtime stack fit together.

async gives you cheap, suspendable functions; they only run when you .await them and the executor polls them.

Compared with one OS thread per bit of concurrency, async tasks are lighter—you can host many concurrent operations without paying for a thread each time.

A minimal async entry point:

1
2
3
4
5
6
7
8
async fn async_fn() {
// handle async logic
}

#[tokio::main]
async fn main() {
async_fn().await
}

Read More

Rust atomics — Atomic types and memory Ordering

深入理解Rust的Atomic及Ordering

Mutex and Condvar are higher-level ways to share data between threads in Rust. Lower down the stack you have atomics — hardware-backed operations on individual locations.

Below: Atomic* types and Ordering, mostly through runnable examples.

Read More

Rust mpsc — sync_channel buffers and unbounded channel

Rust并发控制之Channel

Rust’s sync module exposes mpsc (multi-producer, single-consumer) channels—message passing instead of locking shared memory. That’s the style Rob Pike pushed:

Don’t communicate by sharing memory; share memory by communicating. (R. Pike)

Below: bounded sync_channel and the unbounded channel, plus how Send/Sync show up on the handles.

Read More

Tokio Semaphore — alternating 1 and 2 with two threads

Rust并发控制之Semaphore-两线程交替打印

A semaphore caps how many concurrent actors can use a resource.

Unlike a mutex (exclusive access), a semaphore hands out a fixed number of permits. Think of a bucket of tokens: each visitor acquires a token; when the bucket is empty, new acquires block until someone adds permits or releases them.

Below we use the classic two-thread print puzzle to see how that feels in code.

Read More

Rust Condvar — alternating 1 and 2 with two threads

Rust并发控制之Condvar-两线程交替打印

Interview favorite: two concurrent threads must print 1 and 2 in strict alternation.

Plenty of solutions exist. Here we try condvar, building on the Condvar post.

Read More