Maybe my first crate – a HTML macro for Rust, etc
⚓ Rust 📅 2026-03-03 👤 surdeus 👁️ 1Hi everyone,
I just released my first piece of Rust code: a small HTML macro. I found it useful in some of my projects, and I hope other Rust developers might share my taste.
There are many alternatives out there for HTML macros but there’s always something in the syntax of each of them that I’d like to see done differently. The macro isn’t tied to any particular framework.
A note about proc macros in Rust: it seems like users of a block macro always need to add a few extra symbols so that the compiler sends the block as a string to the macro. In my macro, I went with double brackets ({{ … }}) .
I wonder if Rust could eventually allow something like #[proc_macro_string] so that the user wouldn’t need these extra symbols. Wouldn’t it be nice if the compiler could do that?
Here’s a tiny example of how the macro works:
use kaja_html_macro::html;
fn parent() -> String {
let content = html! {{
<div>
<include child_component_simple_loop() />
</div>
}}
content
}
fn child_component_simple_loop() -> String {
let content = html! {{
<h1>Hello From Nested Component</h1>
<div>
<ul>
<rust>
for i in 1..=3 {
let element_id = format!("list item {:?}", i);
<markup>
<li>Simple $element_id</li>
</markup>
}
</rust>
</ul>
</div>
}}
content
}
Output
<div>
<h1>Hello From Nested Component</h1>
<div>
<ul>
<li>Simple list item 1</li>
<li>Simple list item 2</li>
<li>Simple list item 3</li>
</ul>
</div>
</div>
I’d love feedback from the community — please let me know if anything is done in a “wrong” way or could be improved!
Cheers!
Johan
1 post - 1 participant
🏷️ Rust_feed