Text Processing
Split Once
Read a Key/Value Field
split_once separates text at the first matching delimiter and returns None when it is missing.
Program
Play the program to choose an input line and read the value field if it exists.
split_once_value.rs
Replay: real traced execution (multi-file project)
fn main() {
let line = "port=8080";
let value = parse_value(line).unwrap_or("missing");
println!("{value}");
}
fn parse_value(text: &str) -> Option<&str> {
let (_key, value) = text.split_once('=')?;
Some(value)
}
fn main() {
let line = "mode=fast";
let value = parse_value(line).unwrap_or("missing");
println!("{value}");
}
fn parse_value(text: &str) -> Option<&str> {
let (_key, value) = text.split_once('=')?;
Some(value)
}
fn main() {
let line = "bad";
let value = parse_value(line).unwrap_or("missing");
println!("{value}");
}
fn parse_value(text: &str) -> Option<&str> {
let (_key, value) = text.split_once('=')?;
Some(value)
}
line ← "port=8080"
1fn main() {2 let lin→ "port=8080"e = "port=8080"; //@line="port=8080", "mode=fast", "bad"3 let value = parse_value(lin"port=8080"e).unwrap_or("missing");4 println!("{value}");fn parse_value(text: &str) -> Option<&str>
7fn parse_value(text: &str) -> Option<&str> {8 let (_key, value) = text.split_once('=')?;9 Some(valu"8080"e)10}value ← "8080"
2 let line = "port=8080"; //@line="port=8080", "mode=fast", "bad"3 let valu→ "8080"e = parse_value(lin"port=8080"e).unwrap_or("missing");4 println!("{value}");5}output8080
line ← "mode=fast"
1fn main() {2 let lin→ "mode=fast"e = "mode=fast";3 let value = parse_value(lin"mode=fast"e).unwrap_or("missing");4 println!("{value}");fn parse_value(text: &str) -> Option<&str>
7fn parse_value(text: &str) -> Option<&str> {8 let (_key, value) = text.split_once('=')?;9 Some(valu"fast"e)10}value ← "fast"
2 let line = "mode=fast";3 let valu→ "fast"e = parse_value(lin"mode=fast"e).unwrap_or("missing");4 println!("{value}");5}outputfast
line ← "bad"
1fn main() {2 let lin→ "bad"e = "bad";3 let value = parse_value(lin"bad"e).unwrap_or("missing");4 println!("{value}");value ← "missing"
2 let line = "bad";3 let valu→ "missing"e = parse_value(lin"bad"e).unwrap_or("missing");4 println!("{value}");5}67fn parse_value(text: &str) -> Option<&str> {8 let (_key, value) = text.split_once('=')?;9 Some(value)outputmissing
Follow the Split
linestarts asport=8080.split_once('=')finds the first equals sign.- The key side is
port. - The value side is
8080. unwrap_or("missing")keeps8080, so the program prints8080. | line | split result | printed value | | --- | --- | --- | | port=8080 | keyport, value8080| 8080 | | mode=fast | keymode, valuefast| fast | | bad | no equals sign | missing |
split_once
`split_once('=')` returns the text before and after the first equals sign.
Option
The `?` operator returns `None` when no delimiter exists.
fallback
`unwrap_or("missing")` keeps malformed input visible and deterministic.
Exercise: split_once_value.rs
Reproduce 8080, then use the pinned line variants mode=fast and bad to predict fast and missing.