#string #builder #allocations #length #user-provided

function_string_builder

A string builder that takes a user-provided function

4 stable releases

1.0.3 May 14, 2023
1.0.2 May 13, 2023

#576 in Memory management

41 downloads per month
Used in 2 crates (via remarklib)

MIT license

4KB
57 lines

A new approach to building strings

Instead of maintaining a buffer, this thing minimizes the amount of allocations by calling the user-provided function two times: the first time it sums the lengths of the strings returned using a callback by the function, the second time it pushes said strings into a String that was extended using the lengths sum.

Such approach is aimed at minimizing the amount and size of allocations as much as possible.

An example

assert_eq!(
    build(|mut collector| {
        collector.collect("a");
        collector.collect("bcd");
        collector.collect("ef");
    }),
    "abcdef"
);

No runtime deps