Small text pipelines can trim, lowercase, and replace characters to produce stable identifiers.

Program

Play the program to choose a label and turn it into a simple slug.

label
slugify_label.rs
Replay: real traced execution (multi-file project)
fn main() {
    let label = "Trace UI";
    let slug = label.trim().to_lowercase().replace(' ', "-");
    println!("{slug}");
}
fn main() {
    let label = "Rust Book";
    let slug = label.trim().to_lowercase().replace(' ', "-");
    println!("{slug}");
}
fn main() {
    let label = " Replay ";
    let slug = label.trim().to_lowercase().replace(' ', "-");
    println!("{slug}");
}
  1. label ← "Trace UI", slug ← "trace-ui"

    1fn main() {2    let labe→ "Trace UI"l = "Trace UI"; //@label="Trace UI", "Rust Book", " Replay "3    let slu→ "trace-ui"g = label.trim().to_lowercase().replace(' ', "-");4    println!("{slug}");5}
    outputtrace-ui
  1. label ← "Rust Book", slug ← "rust-book"

    1fn main() {2    let labe→ "Rust Book"l = "Rust Book";3    let slu→ "rust-book"g = label.trim().to_lowercase().replace(' ', "-");4    println!("{slug}");5}
    outputrust-book
  1. label ← " Replay ", slug ← "replay"

    1fn main() {2    let labe→ " Replay "l = " Replay ";3    let slu→ "replay"g = label.trim().to_lowercase().replace(' ', "-");4    println!("{slug}");5}
    outputreplay

Follow the Slug

  1. label starts as Trace UI.
  2. trim() keeps it as Trace UI because there are no outer spaces.
  3. to_lowercase() changes it to trace ui.
  4. replace(' ', "-") changes the space to a hyphen.
  5. The program prints trace-ui. | step | value | | --- | --- | | original label | Trace UI | | lowercase | trace ui | | after replace | trace-ui |
trim `trim` removes leading and trailing whitespace before normalization.
lowercase `to_lowercase` returns an owned `String` with lowercase text.
replace `replace(' ', "-")` swaps spaces for hyphens in the normalized string.

Exercise: slugify_label.rs

Reproduce trace-ui, then use the pinned label variants Rust Book and Replay with outer spaces to predict rust-book and replay.