Horrorshow is a Rust templating library that uses macros to build HTML and XML at compile time, offering type-safe and dependency-light output for Rust projects.
What is Horrorshow?
Horrorshow is a macro-based HTML and XML templating library for stable Rust (requires Rust 1.48 or newer). It accepts Rust source code containing the html! macro—along with helper macros for loops, conditionals, and closures—and produces a string of rendered HTML or XML. The crate is distributed via crates.io, and its API is documented on docs.rs. It is designed to degrade gracefully: when compiled without std, Template::write_to_io() is removed and errors become ToString-compatible; when compiled without alloc, rendering is limited to write_to_fmt() and errors are limited to static &str values.
Key Features
- Macro-based syntax — Build markup inline with
html! { html { head { title : "..." } } }; the compiler validates tag structure at compile time. - Automatic escaping — Text inserted with
:is HTML-escaped; useRaw(...)to emit unescaped content deliberately. - Control-flow integration — Embed
@ for,@ if, and closures directly inside the template to generate repeated or conditional markup. - No-std and no-alloc support — The crate compiles with only
core(no allocation) or withallocbut nostd, with corresponding reductions in availableTemplatemethods. - Template trait — The
Templatetrait provideswrite_to_fmt(),into_string(),write_to_string(), andwrite_to_io(), depending on enabled features. - Helper module — The
helper::doctypemodule outputs standard DOCTYPE declarations, e.g.,doctype::HTMLfor HTML5.
Who is Horrorshow for?
- Rust web developers who want server-side rendered HTML without pulling in a heavyweight template engine, and who value compile-time checks on markup.
- Library authors who need to generate XML or HTML internally (e.g., RSS feeds, sitemaps) and want to stay in pure Rust with no external template dependency.
- Embedded systems programmers working on
no_stdtargets who need minimal HTML/XML generation with no allocation overhead.
What can you do with Horrorshow?
- Generate complete HTML documents: Use
doctype::HTMLplus tags likehtml,head,body,h1,p,ol, andbrto produce a full page, as shown in the README example. - Build lists and loops: Use
@ for i in 0..10to emit repeating elements, with attribute expressions likefirst? = (i == 0)for conditional attributes. - Compose templates from closures: Embed
|tmpl| { tmpl << "..." }blocks to insert output procedurally from Rust code.
How does Horrorshow work?
Horrorshow's macros expand into Rust expressions that implement the Template trait. The rendered output is written to a fmt::Write implementation, allowing you to produce a String via format!("{}", html!{ ... }) or write directly to a Write target. The example in the README demonstrates the entire flow: a macro invocation becomes a string, which is then compared with an expected HTML string in a test.
FAQ
Is Horrorshow free?
Yes, it is a publicly available crate on crates.io and is open source. No pricing tiers or paid features are listed in the repository.
What Rust version is required?
Horrorshow is compatible with stable Rust, with a minimum supported version of Rust 1.48. It is not tied to any nightly features.
Does Horrorshow work without the standard library?
Yes. With the std feature disabled, it compiles without the standard library. If alloc is also disabled, only write_to_fmt() is available and templates may only emit static string errors.
Is Horrorshow an alternative to a front-end framework?
No. Horrorshow only generates HTML/XML strings from Rust code. It does not include JavaScript, CSS, or client-side behavior; you pair it with your own HTTP layer or embed it in a larger application.
What are alternatives to Horrorshow?
Other Rust templating libraries include maud, which also uses an inline macro syntax, and askama, which compiles template files with a syntax inspired by Jinja2. Each has different trade-offs in terms of syntax and compile-time checking.








