Rows for the reading queue may arrive in different formats, such as 7 | Rust Types or 7, Rust Types, so the parser takes the delimiter as an argument. In this checkpoint you write a field reader that returns a borrowed slice of the row, never a copy. The signature has to say that the result comes from the row and not from the delimiter, because in main the delimiter is a temporary String that is gone before the result is used. At the end you keep the title as an owned String, after the row itself has been dropped.
What you will use
- String slices.
split_onceandtrimreturn&strviews into the row, so no text is copied while parsing. - A lifetime parameter on one input.
fn field<'a>(row: &'a str, delimiter: &str) -> Option<&'a str>ties the result torowonly. - Option for "no field". A missing delimiter or a blank field gives
None, not an empty string the caller might store. - An owned copy at the boundary.
String::from(view)keeps the title after the row is dropped.
Plan before you type
- Write the signature first, with
'aonrowand the result only. - Use
row.split_once(delimiter)and keep the second half. It splits at the first delimiter, so the rest of the row stays together. - Trim the field and return
Noneif nothing is left. - Add assertions for a missing delimiter, a blank field and a field that itself contains the delimiter.
- In
main, create the row in a block, create the delimiter in an inner block, parse, then return an owned title from the outer block.
Prove the signature is precise
Change the signature to delimiter: &'a str and run. The parse in main must now fail with E0597, because the delimiter dies before the result is used. That shows your original signature is what allows the temporary delimiter. Also remove your blank-field check: the assertion for "7 | " must fail.
Mistakes worth checking for
error[E0106]: missing lifetime specifier: the signature has two borrowed inputs and a borrowed result, but no lifetime says which input supplies it.- E0597 says the delimiter "does not live long enough": the lifetime was put on both inputs, which ties the result to the delimiter too.
"7 | "returnsSome(""): the blank check runs beforetrim, or not at all."7 | Rust | Types"returns onlyRust: the code split at every delimiter, or at the last one, instead of the first.- E0597 on the row at the end of the outer block: the block returned the slice
viewinstead of an ownedString.
Your turn
Implement field(row, delimiter) to return the trimmed text after the first delimiter, or None when the delimiter is missing or the field is blank. Assert that field("no separator", "|") and field("7 | ", "|") are None, and that field("7 | Rust | Types", "|") is Some("Rust | Types"). Then parse the supplied row with the temporary delimiter, keep an owned title, and print Saved: Rust Types after the row is dropped.
fn main() {
let row = String::from("7 | Rust Types ");
{
let delimiter = String::from("|");
println!("Delimiter: {delimiter}");
}
// Parse with a temporary delimiter, then keep an owned title.
}Hint 1: the idea
The view comes from the row. Only the row needs to outlive the view, so only the row shares the result's lifetime.
Hint 2: a first step
Start with let Some((_, rest)) = row.split_once(delimiter) else { return None; };, which leaves early when there is no delimiter. Then trim rest and check is_empty.
Worked solution
fn field<'a>(row: &'a str, delimiter: &str) -> Option<&'a str> {
let Some((_, rest)) = row.split_once(delimiter) else {
return None;
};
let value = rest.trim();
if value.is_empty() { None } else { Some(value) }
}
fn main() {
assert_eq!(field("no separator", "|"), None);
assert_eq!(field("7 | ", "|"), None);
assert_eq!(field("7 | Rust | Types", "|"), Some("Rust | Types"));
let title = {
let row = String::from("7 | Rust Types ");
let parsed;
{
let delimiter = String::from("|");
parsed = field(&row, &delimiter);
}
let Some(view) = parsed else { panic!("expected a field"); };
String::from(view)
};
println!("Saved: {title}");
}Saved: Rust TypesThe let ... else returns None as soon as split_once finds no delimiter, so the rest of the function only handles a real field. The delimiter drops before view is used, which the signature allows, and the row drops only after view has been copied into owned storage. Day 3 moves from lifetimes to generics: one data shape and one function that work with many types.
Completion is saved in this browser only. It does not measure mastery.