From a9cfe764844d5df432d7b74c5242517280a9d6d0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 1 Jul 2026 22:03:42 +0000 Subject: [PATCH] bolt: optimize string concatenation in casing.rs Co-authored-by: cloudesize67-cmd <237356855+cloudesize67-cmd@users.noreply.github.com> --- .jules/bolt.md | 3 +++ xdk-lib/src/casing.rs | 10 ++++------ 2 files changed, 7 insertions(+), 6 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..2e40d55d --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-07-01 - Avoid intermediate Vec allocations for string concatenation +**Learning:** In Rust, when mapping an iterator of strings and joining them, using `.collect::()` directly is more efficient than `.collect::>().join("")` because it avoids an intermediate heap allocation for the vector. Additionally, for `char::to_lowercase()`, chaining `.to_string()` is preferred over `.collect::()` because it leverages the `Display` implementation and avoids unnecessary iterator collection overhead. +**Action:** When performing string concatenations on iterators, prefer `.collect::()` to aggregate into a single string directly. When converting a `char` to lower/upper case string, use `.to_string()`. diff --git a/xdk-lib/src/casing.rs b/xdk-lib/src/casing.rs index 6463487e..5772196d 100644 --- a/xdk-lib/src/casing.rs +++ b/xdk-lib/src/casing.rs @@ -29,11 +29,8 @@ impl Casing { result } } - Casing::Pascal => words - .iter() - .map(|w| pascal_case(w)) - .collect::>() - .join(""), + // Avoid intermediate Vec allocation by collecting directly to String + Casing::Pascal => words.iter().map(|w| pascal_case(w)).collect::(), Casing::Kebab => words.join("-").to_lowercase(), Casing::ScreamingSnake => words.join("_").to_uppercase(), } @@ -118,6 +115,7 @@ pub fn camel_case(value: &str) -> String { let mut chars = pascal.chars(); match chars.next() { None => String::new(), - Some(first) => first.to_lowercase().collect::() + chars.as_str(), + // Use .to_string() instead of .collect::() for `Display` usage + Some(first) => first.to_lowercase().to_string() + chars.as_str(), } }