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
fn main() {
let line = ;
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 = ;
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 = ;
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)
}
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.