One-character string used where a char pattern is faster
ID |
rust.performance.single_char_pattern |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Rust |
Description
Reports a string-search method given a string literal holding exactly one character, where a char literal expresses the same pattern.
Rationale
A &str pattern searches for a substring, so it runs the general string matcher and rescans on every partial hit. A char pattern compiles to a direct scan for one scalar value, which the standard library can vectorise. On hot paths - splitting every line of a file, trimming every key - the difference shows, and the char form also states that a single character is meant rather than a prefix that happens to be short today.
The following code illustrates the pattern detected by this rule:
pub fn fields(line: &str) -> Vec<&str> {
// FLAGGED: One-character string used where a char pattern is faster
line.split(",").collect()
}