March 24, 2026

Inline asm vs Rust rules: choose your fighter

How to use storytelling to fit inline assembly into Rust

Rust tries to tame wild assembly with a bedtime story—and devs are yelling about it

TLDR: Rust’s new stance: inline assembly and foreign code must respect Rust’s safety rules or risk weird program behavior. The community split: freedom-first coders call it overkill, while safety fans say it’s the only way to keep fast optimizations without breaking apps.

Rust’s compiler guru dropped a think-piece on how low-level “inline assembly” (raw machine code) should behave inside Rust, and the comments lit up like a server rack. In the post, the author shows a tiny example where assembly secretly changes a value that Rust promised wouldn’t change. Result? The program acts differently with and without optimizations—aka Undefined Behavior (UB), the boogeyman of systems code. The takeaway: assembly isn’t a free pass; it has to play by Rust’s story about memory and safety.

Cue the drama. One camp cheered, saying “optimizations need rules” and the compiler can’t read your mind. Another camp roasted the idea as “storytime lawyering,” grumbling that Rust is becoming a philosophy class where “Tree Borrows” (rules about who can touch memory) glares at your every move. The hot take factory pumped out memes: “Schrödinger’s assert” (fails in debug, passes in release), “Narrator: it was UB,” and “inline asm is a cheat code with parental controls.”

The spiciest thread: Should Rust trust assembly “grown-ups,” or enforce house rules at the door? Performance diehards want freedom; safety fans want consistency. FFI (calling non-Rust code) got dragged into the brawl, too. One top comment summed it up: “If you invite the dragon in, don’t be shocked when the curtains burn.”

Key Points

  • Inline assembly and FFI must adhere to Rust’s Abstract Machine semantics to keep compiler optimizations sound.
  • An example shows asm writing through a shared reference, which the compiler assumes is non-mutating, causing divergent behavior with and without optimizations.
  • To preserve desired optimizations, such asm must be considered Undefined Behavior under Rust’s aliasing models.
  • Models like Tree Borrows and Stacked Borrows prohibit writes through pointers derived from shared references in pure Rust.
  • Defining Tree Borrows for assembly is infeasible due to lack of pointer provenance in asm; a general principle will relate Rust semantics to asm without re-implementing the model.

Hottest takes

“Schrödinger’s assert: fails on debug, passes on release—aka the UB multiverse” — optimizer_enjoyer
“Stop turning memory models into fanfic; I just want to flip bits without a priest” — segfault_sally
“Assembly is a house guest; follow the rules or don’t touch the furniture” — bit_bard
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.