8 stable releases

2.4.0 Mar 22, 2023
2.3.0 Mar 1, 2023
2.2.0 Dec 1, 2022
2.1.1 Jul 28, 2022
1.0.0 Jan 24, 2021

#23 in Template engine

Download history 917/week @ 2024-01-06 642/week @ 2024-01-13 763/week @ 2024-01-20 706/week @ 2024-01-27 1007/week @ 2024-02-03 875/week @ 2024-02-10 1139/week @ 2024-02-17 812/week @ 2024-02-24 1193/week @ 2024-03-02 860/week @ 2024-03-09 879/week @ 2024-03-16 762/week @ 2024-03-23 1565/week @ 2024-03-30 648/week @ 2024-04-06 1126/week @ 2024-04-13 1011/week @ 2024-04-20

4,496 downloads per month
Used in 10 crates (8 directly)

MIT license



build_html: Rust HTML Generation

This crate allows HTML strings to be generated from within Rust code using the Builder pattern. In general, this library will attempt to create a 'minimal' version of the HTML; tags are concatinated without additional spaces being added. For this reason, this library may not be optimal for use in applications intending to display the raw HTML.

The main application for this crate, and its impetus, is providing simple server-side rendering for a web application. However, it could also be used to generate HTML reports from within other applications.

This crate is written in purely safe Rust with no production dependencies.


Everything you need to use this crate has been exported from the crate root. This means that you can get easy access to every element using the import: use build_html::*;.

While the easiest way to use the library is with a root import, you can also import items piecemeal and prefix types with the package name. Note that the traits Html and HtmlContainer must be in scope for the library to be useful:

use build_html::{self, Html, HtmlContainer};

let page = build_html::HtmlPage::new()
    .with_paragraph("Some Text")

This project was created with the builder pattern in mind. To create an HTML document, start with an HtmlPage, and build up the document with chained method calls. Once the document is built up, convert it to a String using to_html_string(). The Display trait is also implemented for all Html elements, meaning these elements can be converted into HTML using format!() as well.

While adding content, required attributes are specified as method parameters. These parameters are generic on the ToString trait, allowing many useful types to be used. Additional optional parameters (such as id or class attributes) can be added by passing in some type implementing the IntoIterator trait which has items which are 2-tuples of objects implementing ToString. This means that you can use anything from a HashMap<String, String> to a Vec<(&str, &str)> to arrays of 2-tuples of static strings.

use build_html::*;

let html: String = HtmlPage::new()
    .with_title("My Page")
    .with_header(1, "Main Content:")
            .with_attributes([("id", "article1")])
            .with_header_attr(2, "Hello, World", [("id", "article-head"), ("class", "header")])
            .with_paragraph("This is a simple HTML demo")

produces a string equivalent to:

<!DOCTYPE html>
        <title>My Page</title>
        <h1>Main Content:</h1>
        <article id="article1">
            <h2 id="article-head" class="header">Hello World</h2>
            <p>This is a simple HTML demo</p>

If you are trying to create a more complicated document, you may find it easier to create a template from outside of Rust and build it into the binary with the include_str!() macro. Then, you can generate the parts that need to be dynamically created with build_html and insert them with the format!() macro.

Supported Features

This library does not intend to support all tags and features from the HTML specification. However, an effort has been made to provide the most common features for a majority of use cases.

Currently, this library supports adding the following HTML features / tags:

Body Elements

  • Containers (<article>, <div>, <main>)
  • Headers (<h1>, <h2>, <h3>, ... )
  • Images (<img>)
  • Links (<a>)
  • Lists (<li>, <ol>)
  • Paragraphs (<p>)
  • Preformatted Text (<pre>)
  • Tables (<table>, <tr>, <th>, <td>)

Header Elements

  • Links (<link>)
  • Meta (<meta>)
  • Scripts (<script>)
  • Style (<style>)
  • Title (<title>)


In the event that you require additional tags or types not implemented in this library, you can achieve this using one of two escape hatches.

  1. You can implement your own HTML types and add them using HtmlContainer::add_html
  2. You can add one-off raw content using HtmlContainer::add_raw


If you have an idea for making this library better, feel free to open an issue or pull request on GitHub! I try to respond within a reasonable amount of time, but please keep in mind that maintaining this library is not my full time job.


This project was made possible thanks to the following great projects:


This project is licensed under the MIT license.

Copyright (C) 2020-23 Joseph Skubal and Contributors

No runtime deps