Skip to main content

safe_html_format_to

Macro safe_html_format_to 

Source
macro_rules! safe_html_format_to {
    ($w:expr, $fmt:literal $(,)?) => { ... };
    ($w:expr, $fmt:literal, $($name:ident = $val:expr),+ $(,)?) => { ... };
    ($w:expr, $fmt:literal, $($val:expr),+ $(,)?) => { ... };
}
Expand description

Streams a trusted template literal with escaped interpolations into any core::fmt::Write — the no_std, zero-allocation sibling of safe_html_format!.

The first argument is the writer; the format string and arguments follow the same rules as safe_html_format! (compile-time-checked placeholders, arguments routed through SafeHtmlPart). Evaluates to core::fmt::Result, so a full SliceWriter surfaces as Err.

The caller vouches for the destination: whatever is written is safe HTML, but a raw writer (e.g. a String) does not itself carry the SafeHtml contract. Use SafeHtmlBuilder (with safe_html_format!(builder, ...)) to keep the contract in the type.

use safehtml::{SliceWriter, safe_html_format_to};

let mut buf = [0u8; 64];
let mut w = SliceWriter::new(&mut buf);
safe_html_format_to!(w, "<b>{msg}</b>", msg = "5 < 6").unwrap();
assert_eq!(w.as_str(), "<b>5 &lt; 6</b>");