| Crates.io | fast-cat |
| lib.rs | fast-cat |
| version | 0.1.1 |
| created_at | 2025-06-28 16:12:36.458591+00 |
| updated_at | 2025-06-28 16:30:07.7331+00 |
| description | An ergonomic macro for efficient string concatenation with a single memory allocation. |
| homepage | |
| repository | https://github.com/vloldik/fast-cat |
| max_upload_size | |
| id | 1729950 |
| size | 34,989 |
fast-catAn ergonomic macro for highly efficient string concatenation with a single memory allocation.
fast-cat?Standard methods like format! or using the + operator can be inefficient for joining multiple strings, often leading to several memory re-allocations. This crate provides a concat_str! macro that solves this problem by first calculating the total size of all string parts and then performing only a single memory allocation.
It gives you the performance of a manually optimized String::with_capacity approach but with a much cleaner and more convenient syntax.
format! and + for string concatenation (see benchmarks below).Add this to your Cargo.toml:
[dependencies]
fast-cat = "0.1.0" # Replace with the latest version
The concat_str! macro accepts any number of comma-separated arguments that can be referenced as a &str, including string literals, String variables, and &str slices.
use fast_cat::concat_str;
fn main() {
let world = "world".to_string();
let excitement = "!";
// Mix and match literals, owned Strings, and slices
let greeting = concat_str!("Hello, ", &world, excitement);
assert_eq!(greeting, "Hello, world!");
}
Benchmarks show that concat_str! is not only significantly faster than standard library alternatives but also manages to outperform a hand-written, manually optimized implementation.
The test scenario involved concatenating 6 string parts of mixed types (&str and String).
| Method | Average Time (ns) | Relative Speed (Compared to concat_str!) |
|---|---|---|
✅ concat_str! (Ours) |
47.8 ns | 1.0x |
manual_with_capacity |
54.2 ns | Same |
[T]::concat() |
69.6 ns | ~1.5x slower |
addition (+) |
222.7 ns | ~4.7x slower |
format! |
256.1 ns | ~5.4x slower |
Results were obtained on a specific machine and may vary. The relative performance is the key takeaway.
As the data shows, concat_str! provides the best performance, making it an ideal choice for performance-sensitive applications.
This project is licensed under either of
at your option.