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.

line
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)
}
  1. 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}");
  2. 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}
  3. 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
  1. 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}");
  2. 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}
  3. 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
  1. line ← "bad"

    1fn main() {2    let lin→ "bad"e = "bad";3    let value = parse_value(lin"bad"e).unwrap_or("missing");4    println!("{value}");
  2. 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

  1. line starts as port=8080.
  2. split_once('=') finds the first equals sign.
  3. The key side is port.
  4. The value side is 8080.
  5. unwrap_or("missing") keeps 8080, so the program prints 8080. | line | split result | printed value | | --- | --- | --- | | port=8080 | key port, value 8080 | 8080 | | mode=fast | key mode, value fast | 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.