Review generated record view

from Lifetimes
Rust 1.98 advanced 6 min 5 issues to find

Review this generated record lookup helper.

Find a key in semicolon-separated fields, return an optional zero-copy view into record, permit temporary key and delimiter arguments, trim Unicode whitespace, allocate nothing, and produce no output.

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

    for field in owned.split(';') {
        if let Some((candidate, value)) = field.split_once(delimiter) {
            if candidate.trim_ascii() == key {
                return value.trim_ascii();
            }
        }
    }

    "missing"
}

generated code is illustrative, not from any one model

Open in playground
Report an error