Review generated borrowed key view

from Lifetime elision and annotations
Rust 1.98 advanced 6 min 5 issues to find

Review this generated borrowed-key helper.

Return the key before a separator as a view into record, allow a temporary separator, return the whole record when no separator exists, trim Unicode whitespace, allocate nothing, and produce no output.

Rust
fn extract_key<'a>(
    record: &'a str,
    separator: &'a str,
) -> &'a str {
    println!("record={record}");
    let owned = record.to_string();

    let candidate = if let Some((key, _)) = owned.split_once(separator) {
        key
    } else {
        "missing"
    };

    if candidate.is_empty() {
        return "missing";
    }

    candidate.trim_ascii()
}

generated code is illustrative, not from any one model

Open in playground
Report an error