Rust guides
Rust guides: async runtimes and embedded targets, with what actually runs under QEMU.
-
Fix Rust E0308 "mismatched types" — Convert the Value, Fix the Signature, or Give Both Sides One Type
Rust error E0308, mismatched types, means an expression produced one type where a different type was required: an i32 passed where an i64 belongs, if and else arms that disagree, a String where a &str belongs or the reverse, a value where a reference belongs, a stray semicolon turning a return value into (). This guide reproduces the error in its argument, return, branch, string, reference, and unit forms, shows the narrow set of conversions Rust performs automatically and the much larger set it refuses, and gives the fixes in the order to reach for them: change the type at the source, convert with From or Into where the conversion cannot lose data, try with TryFrom where it can, cast with as only with the loss priced in, and borrow or dereference where the mismatch is about access rather than value.
-
Fix Rust E0502 "cannot borrow as mutable because it is also borrowed as immutable" — End the First Borrow, Split the Work, or Restructure
Rust error E0502, cannot borrow as mutable because it is also borrowed as immutable, means a shared borrow is still alive at the point where the code asks for a mutable borrow. This guide reproduces the error in its canonical form, its loop form, and its method-call form, shows the near-identical program the current compiler accepts because the first borrow has already ended, and gives the fixes in the order to reach for them: end or narrow the first borrow, split the work with an index loop or split_at_mut, restructure so a value is returned instead of a reference held, and clone only as a deliberate last resort.
-
Fix Rust E0382 "borrow of moved value" — Borrow Instead of Moving, Return the Value, or Clone on Purpose
Rust error E0382, borrow of moved value, means a value was moved — into a function, another binding, a loop, or a closure — and the code then tried to use the original binding, which no longer owns anything. This guide reproduces the error in its function-call, assignment, loop, closure, and partial-move forms, shows exactly which uses survive a partial move and which do not, and gives the fixes in the order to reach for them: borrow instead of moving, return the value or restructure, take or replace a field, derive Copy only where copying is semantically right, and clone or share ownership only as a deliberate choice.
-
Rust Structs and Enums — Named Fields, Variants, and match
Rust structs and enums: a Point struct prints x=3 y=4, a Shape enum carries a radius in one variant and named width and height fields in another, and match prints circle area=12.57 and rect area=12.00. The same guide shows the compiler rejecting a match with a missing arm (error E0004), Option as a built-in enum, and struct update syntax moving a String field (error E0382 when the moved field is read again).
-
Rust Ownership and Borrowing — Moves, Borrows, and Clones
The one idea Rust beginners must get first: each value has one owner, assignment moves a String and copies an i32, a borrow lets a function read without taking ownership, and clone makes a second owner. One working program and the exact E0382 error.
-
Bare-Metal STM32 in Rust: No HAL, No Embassy — and It Actually Runs Under QEMU
A three-crate Rust firmware with no HAL and no Embassy builds in 3.18 seconds and prints under QEMU on both machines, while the HAL app hangs at init. The measured sizes, the timed loop counts, and the fault matrix for a wrong flash base.
-
Rust Embassy on STM32: What Compiles, What Runs, and the QEMU Gap
A real Embassy program for the STM32F103C8 compiles with embassy-stm32 0.4.0, and an executor-only firmware prints under QEMU. The full app does not run under QEMU: embassy_stm32::init() hangs on the emulated board, and QEMU's STM32F100 machine faults on the same binary.
-
Tokio Backpressure: Bounded Channels and Semaphore-Limited HTTP Fan-Out
Bound a Tokio pipeline at both ends: a bounded mpsc channel that parks the sender, and a semaphore whose placement decides whether 50 tasks exist or 5 do — tested with Tokio 1.53.2 and Rust 1.99.1.
-
Rust Lazy Statics, Measured: OnceLock vs LazyLock vs lazy_static — and the 2x Claim That Does Not Reproduce
Three ways to build a lazy static in Rust, measured: steady-state access and contended first init. OnceLock is 1.24x faster than lazy_static, not 2x.