Rust text processing: String, &str and the regex crate

Rust has two everyday string types and a small set of standard-library methods that cover most text work. When you need pattern matching, the regex crate fills the gap. This post walks through both, with examples you can paste into cargo run.
String vs &str
Section titled “String vs &str”Both hold UTF-8 encoded text. The difference is ownership:
Stringis an owned, growable buffer on the heap. Use it when you build or modify text.&stris a borrowed view into UTF-8 bytes owned by something else: aString, a string literal (which is&'static str), or a slice of either. Use it for read-only access, and prefer&strfor function parameters, because a&Stringcoerces to&strautomatically.
Building a String
Section titled “Building a String”The common methods live on the String type in std::string:
String::new()creates an empty string;String::from("...")or"...".to_string()copies a literal.push_str(&str)appends a slice;push(char)appends one character.pop()removes and returns the lastchar, orNoneif the string is empty.format!builds a newStringfrom a template, which is usually clearer than many pushes.
fn main() { let mut greeting = String::new(); greeting.push_str("Hello, "); greeting.push_str("Rust"); greeting.push('!'); println!("{greeting}"); // Hello, Rust!
let last = greeting.pop(); println!("{last:?} {greeting}"); // Some('!') Hello, Rust
let name = "Ferris"; let message = format!("{greeting} and {name}"); println!("{message}"); // Hello, Rust and Ferris}Reading and splitting &str
Section titled “Reading and splitting &str”Methods on str work on both &str and String (through deref):
trim()removes leading and trailing whitespace and returns a subslice, with no allocation.split(pat)splits on achar,&stror closure;split_whitespace()splits on any run of Unicode whitespace.lines()iterates over lines, handling both\nand\r\n.chars()iterates over Unicode scalar values (char);bytes()iterates over rawu8.
fn main() { let input = " Hello, Rust! \n";
// split(' ') would yield empty strings for the repeated spaces; // split_whitespace() skips them. let words: Vec<&str> = input.trim().split_whitespace().collect(); println!("{words:?}"); // ["Hello,", "Rust!"]
let csv = "name,age\nAda,36\r\nLinus,54"; for line in csv.lines().skip(1) { let fields: Vec<&str> = line.split(',').collect(); println!("{} is {}", fields[0], fields[1]); }}len() counts bytes, not characters
Section titled “len() counts bytes, not characters”This is the gotcha that catches most people coming from other languages:
fn main() { let s = "héllo"; println!("{}", s.len()); // 6: 'é' takes two bytes in UTF-8 println!("{}", s.chars().count()); // 5
// Slicing uses byte offsets and panics if an offset falls inside a character: println!("{}", &s[0..1]); // "h" // println!("{}", &s[0..2]); // panics: byte index 2 is not a char boundary
// Use get() when you're not sure; it returns None instead of panicking. println!("{:?}", s.get(0..2)); // None}A char is a Unicode scalar value, not a user-perceived character. An emoji with a skin-tone modifier, or a letter with a combining accent, is several chars. If you need grapheme clusters (for cursor movement or truncating text for display), use the unicode-segmentation crate.
Pattern matching with regex
Section titled “Pattern matching with regex”The standard library has no regular expressions. The regex crate, maintained by the Rust project, is the usual choice. Add it with cargo add regex, or in Cargo.toml:
[dependencies]regex = "1.12"The main API:
Regex::new(pattern)compiles a pattern and returnsResult<Regex, regex::Error>.is_match(text)checks whether the pattern matches anywhere.find_iter(text)yields each match;captures(text)/captures_iter(text)give access to capture groups.replace_all(text, rep)returns aCow<str>, which only allocates if something was replaced.
use regex::Regex;
fn main() { let text = "I have 3 apples and 4 oranges.";
let digits = Regex::new(r"\d+").unwrap(); println!("{}", digits.is_match(text)); // true println!("{}", digits.replace_all(text, "X")); // I have X apples and X oranges.
// Named capture groups: (?<name>...) is supported since regex 1.9. let item = Regex::new(r"(?<count>\d+) (?<fruit>\w+)").unwrap(); for caps in item.captures_iter(text) { let count: u32 = caps["count"].parse().unwrap(); println!("{} -> {}", &caps["fruit"], count); } // apples -> 3 // oranges -> 4}Gotchas
Section titled “Gotchas”- Compile a regex once.
Regex::newis relatively expensive. Don’t call it inside a loop or a hot function; store it in a struct or astaticusingstd::sync::LazyLock(stable since Rust 1.80). - No look-around or backreferences. The
regexcrate guarantees linear-time matching, so it leaves out features that need backtracking. If you need them,fancy-regexadds them on top. - Plain substring work doesn’t need regex.
contains,starts_with,find,replaceandsplitonstrare simpler and faster for fixed strings.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)