olav
A compile-time XML template engine for Rust, inspired by maud.
Example: an Atom feed
use ;
let feed_title = "Example Feed";
let feed_id = "urn:uuid:60a76e80-d399-11e9-b23e-2a8991f4d4ad";
let updated = "2026-08-22T18:30:02Z";
let author_name = "Jane Developer";
let author_email = "jane@example.com";
let is_published = true;
let entries: = vec!;
let feed = xml! ;
println!;
Produces:
<!-- Atom 1.0 feed, generated by olav -->
Example Feed
An olav example feed.
urn:uuid:60a76e80-d399-11e9-b23e-2a8991f4d4ad
2026-08-22T18:30:02Z
Jane Developer
jane@example.com
© 2026 Example Inc.
Atom-Powered Robots Run Amok
urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a
2026-08-22T18:30:02Z
An atom-sized summary.
Macros in Rust
urn:uuid:b3f5df58-2f55-4b56-9c52-9d6e0d3d7a8b
2026-08-15T10:00:00Z
An atom-sized summary.
Features
- Compile-time checking — malformed templates fail at build time, including
well-formedness checks: invalid XML names,
--in comments,]]>in CDATA,?>in processing instructions, duplicate attributes. - Auto-escaping — text and attribute values are XML-escaped by default.
- Unicode names — Cyrillic and any Rust-identifier names work:
<книга>. - Namespaces — bracketed names for any element/attribute:
[svg:rect],[xml:lang]. - Full XML — processing instructions, DOCTYPE with internal subset, CDATA, comments.
- Control flow —
@if/@else,@for,@while,@match. - Splices —
@expr(escaped) and@expr.raw(verbatim);PreEscapedfor raw insertion. - Rich
Renderimpls —&str,String, integers, floats,bool,Option<T>,Vec<T>,[T; N], plus aDisplayValuewrapper for anystd::fmt::Displaytype. - Zero-cost sizing — the output buffer is pre-sized from template literals at compile time.
Unicode and Cyrillic names
use xml;
let author = "Лев Толстой";
let year: i32 = 2026;
let library = xml! ;
println!;
Produces (UTF-8 output):
Война и мир
роман
Анна Каренина
роман
роман
повесть
рассказ
Works for any Unicode script that Rust accepts as an identifier: Cyrillic, Greek, CJK, Arabic, Hebrew, etc. Mixed Cyrillic + Latin + numbers in the same document is fine.
For non-standard names (hyphens, colons), use the bracketed form:
[my-element] { }, [svg:rect] { }. Bracketed names must still be valid
XML names — they have to start with a letter, _, or :; names like
[1st] are rejected at compile time.
Syntax overview
Elements
book // <book>text</book>
book // <book/>
// <ns:book>x</ns:book>
// <my-element/>
книга // <книга>текст</книга>
Attributes (in parentheses)
book // attributes are wrapped in (...)
| Form | Result |
|---|---|
id="literal" |
id="literal" |
id=value |
id="<Display(value)>" (escaped) |
id? |
if cond: id="true" else omit |
id?=value |
if cond: id="<value>" else omit |
[ns:attr]="value" |
namespaced attribute, e.g. [xlink:href], [xml:lang], [data-x] |
Body and splices
"text" // escaped text
@expr // splice (Render, escaped)
@expr.raw // splice, no escaping
@cdata // <![CDATA[raw < > &]]>
Control flow
@if cond @else
@for pat in iter
@while cond
@match expr
XML constructs
?xml version="1.0" encoding="UTF-8" // <?xml ...?>
?xml version="1.0" encoding="UTF-8" standalone="yes" // with standalone
?xml-stylesheet type="text/xsl" href="style.xsl"
!DOCTYPE html // <!DOCTYPE html>
!DOCTYPE name SYSTEM "x.dtd"
!DOCTYPE name PUBLIC "id" "url"
!DOCTYPE root
@comment "this is a comment" // <!--this is a comment-->
Escape hatch
For pre-escaped or pre-built markup, use PreEscaped:
use ;
let raw = "<b>already escaped</b>";
let m = xml! ;
To render third-party types that implement std::fmt::Display, use
DisplayValue (output is escaped as usual):
use ;
use Ipv4Addr;
let ip = new;
let m = xml! ;
Why no /* */ for comments?
Rust's lexer strips block comments before they reach proc-macros, so
/* ... */ is not usable inside xml! { ... }. Use @comment "..." instead.
License
MIT — see LICENSE-MIT.