#string #concat #fastest #macro #concatenating

no-std fast-concat

Fastest macro for concatenating strings

3 stable releases

new 1.0.2 Feb 16, 2025
1.0.0 Dec 10, 2024

#696 in Rust patterns

Download history 127/week @ 2024-12-10 1/week @ 2024-12-17 9/week @ 2024-12-31 218/week @ 2025-02-11

218 downloads per month
Used in rpgn

MIT/Apache

9KB
77 lines

The fastest, constant able (thanks to constcat), no-std compatible way to concatenate &strs. Due to how macros work, you need to import constcat as a separate dependency.

Comparison with other macros

This is as fast or faster than the fastest string concatenating crates (I checked those in hoodie/concatenation_benchmarks-rs).

Those have other problems too:

  • concat_string_macro evaluates expressions twice and requires std.
  • concat_strs_macro doesn't work for certain expressions.
  • string_concat_macro is the best, but it doesn't have the last two of the optimizations below. As a nitpick, it also requires that you use string_concat::string_concat_impl. I know, I know. Grasping at straws, but I wanted to go over all the differences.

Optimizations

  • Each expression gets a variable and thus won't be evaluated twice at runtime.
  • If you pass multiple constants or literals in a row, they will be concatenated at compile time and pushed to the buffer only once.
  • Passing only constants or literals makes the macro act as the concat! macro, and a literal will be returned.

Dependencies

~195–620KB
~15K SLoC