Could Rust adding extra indent with format! macro?

⚓ Rust    📅 2025-08-21    👤 surdeus    👁️ 3      

surdeus

I'm writting a program that generate C# code using Rust, For example I have:

namespace Generated
{
    public partial class Generated1
    {
        // things
    }
    public partial class Generated2
    {
        // things
    }
}

Since Generated1 and Generated2 are complex, there is a single function that generate classes like Generated*, which yields the class without indent:

// println!("{}", generating(1)) yields:
public partial class Generated1
{
    // things
}

I wonder is there some technique that allow formatting the generated items with extra indent. Since writting indent in the generating function seems a lot of chaos:

    // with indent
    format!(
r#"{space:width$}public partial class Generated1
{space:width$}{{
{space:width_plus_1$}// things
{space:width$}}}"#, space = "", width = indent_level * 4, width_plus_1 = indent_level * 4);
    // without indent
    format!(
r#"public partial class Generated1
{{
    // things
}}"#, space = "", width = indent_level * 4, width_plus_1 = indent_level * 4);

Is there some technique to format items with extra indent available in Rust?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed