Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -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::<String>()` directly is more efficient than `.collect::<Vec<_>>().join("")` because it avoids an intermediate heap allocation for the vector. Additionally, for `char::to_lowercase()`, chaining `.to_string()` is preferred over `.collect::<String>()` because it leverages the `Display` implementation and avoids unnecessary iterator collection overhead.
**Action:** When performing string concatenations on iterators, prefer `.collect::<String>()` to aggregate into a single string directly. When converting a `char` to lower/upper case string, use `.to_string()`.
10 changes: 4 additions & 6 deletions xdk-lib/src/casing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ impl Casing {
result
}
}
Casing::Pascal => words
.iter()
.map(|w| pascal_case(w))
.collect::<Vec<_>>()
.join(""),
// Avoid intermediate Vec allocation by collecting directly to String
Casing::Pascal => words.iter().map(|w| pascal_case(w)).collect::<String>(),
Casing::Kebab => words.join("-").to_lowercase(),
Casing::ScreamingSnake => words.join("_").to_uppercase(),
}
Expand Down Expand Up @@ -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::<String>() + chars.as_str(),
// Use .to_string() instead of .collect::<String>() for `Display` usage
Some(first) => first.to_lowercase().to_string() + chars.as_str(),
}
}
Loading