7 unstable releases

Uses new Rust 2024

0.3.0 Dec 6, 2025
0.2.0 Sep 19, 2022
0.1.3 May 9, 2020
0.1.1 Mar 15, 2019
0.0.0 Oct 31, 2017

#134 in Rust patterns

Download history 9578/week @ 2025-10-14 8660/week @ 2025-10-21 7175/week @ 2025-10-28 8687/week @ 2025-11-04 9197/week @ 2025-11-11 10158/week @ 2025-11-18 8596/week @ 2025-11-25 10565/week @ 2025-12-02 9141/week @ 2025-12-09 10737/week @ 2025-12-16 6377/week @ 2025-12-23 5389/week @ 2025-12-30 9788/week @ 2026-01-06 11810/week @ 2026-01-13 10056/week @ 2026-01-20 11645/week @ 2026-01-27

44,712 downloads per month
Used in 71 crates (43 directly)

MIT license

72KB
2K SLoC

Codegen

Provides a builder API to assist in generating Rust code.

More information about this crate can be found in the crate documentation

Discord

Installation

To use codegen, first add it as a dependency

cargo add codegen

Usage

  1. Create a Scope instance.
  2. Use the builder API to add elements to the scope.
  3. Call Scope::to_string() to get the generated code.

Example

use codegen::Scope;

let mut scope = Scope::new();

scope.new_struct("Foo")
    .derive("Debug")
    .field("one", "usize")
    .field("two", "String");

println!("{}", scope.to_string());

Non-goals

codegen will not attempt to perform anything beyond basic formatting. For improved formatting, the generated code can be passed to rustfmt.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in codegen by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~1MB
~15K SLoC