Review generated name summary

from Strings
Rust 1.98 advanced 6 min 4 issues to find

Review this generated display-name summarizer.

Trim comma-separated names, reject the first empty field, limit each name to max_chars Unicode scalar values without panicking, preserve order, and produce no output.

Rust
fn summarize_names(input: &str, max_chars: usize) -> String {
    let mut result = String::new();

    for raw in input.split(',') {
        let mut name = raw.trim().to_string();
        if name.len() > max_chars {
            name.truncate(max_chars);
        }

        if name.is_empty() {
            continue;
        }

        if !result.is_empty() {
            result.push_str(", ");
        }

        println!("adding {name}");
        result.push_str(&name);
    }

    result
}

generated code is illustrative, not from any one model

Open in playground
Report an error