Question
In Rust, how can I split a string into parts using a delimiter?
For example, in Java you might write:
"some string 123 ffd".split("123");
What is the Rust equivalent, and how does string splitting work in Rust?
Short Answer
By the end of this page, you will understand how to split strings in Rust using .split(), what kind of values it returns, how to collect the results, and how to use common related methods such as split_once, split_whitespace, and lines in practical code.
Concept
Rust provides string-splitting methods on str, the string slice type. The most common one is .split(...), which separates a string wherever a given pattern appears.
For example:
let text = "some string 123 ffd";
let parts: Vec<&str> = text.split("123").collect();
This produces pieces of the original string without copying the text. That is an important Rust idea:
split()returns an iterator- each item is usually a
&str - those
&strvalues are slices pointing into the original string
This matters because Rust tries to avoid unnecessary memory allocation. Instead of creating brand new strings immediately, it gives you borrowed views into the original string.
You can then:
- loop through the pieces
- collect them into a
Vec<&str> - take only the first few pieces
- parse the pieces into other types
String splitting is common in real programming because text data often comes in structured forms such as:
- CSV-like input
- log lines
- URLs
- configuration files
- command-line input
- API responses
Understanding Rust string splitting also helps you understand two core Rust ideas:
Mental Model
Think of a string as a long strip of paper.
When you use split(), Rust does not immediately make brand new strips of paper. Instead, it marks the positions where the delimiter appears and gives you views of each section.
So if the string is:
some string 123 ffd
and the delimiter is 123, Rust sees:
[some string ][ ffd]
The results are slices into the original text, like pointing at sections of the same paper rather than photocopying them.
This is why splitting is efficient in Rust: you usually borrow pieces first, and only create owned String values if you actually need them.
Syntax and Examples
Basic syntax
let text = "some string 123 ffd";
let parts = text.split("123");
parts is an iterator, not a vector yet.
Collecting into a vector
let text = "some string 123 ffd";
let parts: Vec<&str> = text.split("123").collect();
println!("{:?}", parts);
Output:
["some string ", " ffd"]
Splitting by a single character
let csv = "red,green,blue";
let colors: Vec<&str> = csv.split().();
(, colors);
Step by Step Execution
Consider this example:
let text = "some string 123 ffd";
let parts: Vec<&str> = text.split("123").collect();
println!("{:?}", parts);
Step 1: Create the string slice
let text = "some string 123 ffd";
text is a &str, a borrowed string slice.
Step 2: Call split
text.split("123")
Rust searches for every occurrence of "123" in the string.
It finds this structure:
- before
123:"some string " - after
123:
Real World Use Cases
Parsing configuration values
let line = "host=localhost";
if let Some((key, value)) = line.split_once('=') {
println!("{} -> {}", key, value);
}
Useful for simple .env-style or config files.
Reading CSV-like data
let row = "alice,30,developer";
let fields: Vec<&str> = row.split(',').collect();
Useful when reading simple comma-separated records.
Processing log lines
let log = "INFO: Server started";
if let Some((level, message)) = log.split_once(':') {
println!("level={} message={}", level.(), message.());
}
Real Codebase Usage
In real Rust projects, developers usually combine string splitting with iterator methods and validation.
Common patterns
Guard clauses with split_once
fn parse_pair(line: &str) {
let Some((key, value)) = line.split_once('=') else {
println!("Invalid input");
return;
};
println!("{} = {}", key, value);
}
This avoids nested if statements and handles invalid input early.
Trimming after splitting
let line = "name = alice";
if let Some((key, value)) = line.split_once('=') {
let key = key.trim();
let value = value.trim();
println!("{} -> {}", key, value);
}
Common Mistakes
Mistake 1: Expecting split() to return a vector directly
Broken idea:
let parts = "a,b,c".split(',');
println!("{:?}", parts);
Why it is a problem:
split()returns an iterator- the iterator itself is not the final list of parts you may expect
Fix:
let parts: Vec<&str> = "a,b,c".split(',').collect();
println!("{:?}", parts);
Mistake 2: Using split(' ') when whitespace may vary
Broken code:
let text = "Rust is great";
let parts: Vec<&str> = text.().();
(, parts);
Comparisons
| Method | What it does | Best use case |
|---|---|---|
split(delimiter) | Splits on every match of a pattern | General-purpose splitting |
split_once(delimiter) | Splits into two parts at the first match | Key/value pairs like a=b |
split_whitespace() | Splits on any amount of whitespace | User input, words in text |
lines() | Splits by line boundaries | Reading multiline text |
rsplit(delimiter) | Splits from the right | Parsing suffixes or file extensions |
split vs split_once
Cheat Sheet
// Split into many parts
let parts: Vec<&str> = "a,b,c".split(',').collect();
// Split using a string delimiter
let parts: Vec<&str> = "one--two--three".split("--").collect();
// Split into two parts only
let pair = "key=value".split_once('=');
// Split on whitespace
let words: Vec<&str> = "a b\tc".split_whitespace().collect();
// Split into lines
let lines: Vec<&str> = "a\nb\nc".lines().collect();
// Remove empty parts
let parts: Vec<&> = .().(|s| !s.()).();
: <> = .().(::from).();
FAQ
How do I split a string by a word in Rust?
Use .split("word"):
let parts: Vec<&str> = "abcWORDxyz".split("WORD").collect();
Does Rust split() return a Vec?
No. It returns an iterator. Use .collect() to turn it into a Vec.
How do I split a string only once in Rust?
Use .split_once(delimiter). It returns Option<(&str, &str)>.
How do I split by spaces in Rust?
You can use .split(' '), but .split_whitespace() is usually better because it handles multiple spaces, tabs, and newlines.
Why are the split results &str instead of String?
Because Rust borrows slices from the original string to avoid unnecessary allocations.
Mini Project
Description
Build a small Rust parser for simple key=value configuration lines. This project demonstrates string splitting in a realistic format that appears in environment files, settings files, and command-line tools.
Goal
Create a program that reads several key=value lines, splits each line safely, trims whitespace, skips invalid lines, and prints the parsed key-value pairs.
Requirements
- Store a few sample configuration lines in a collection.
- For each line, split only once on the
=character. - Trim whitespace around both the key and value.
- Skip lines that do not contain
=. - Print each valid key-value pair in a clear format.
Keep learning
Related questions
Accessing Cargo Package Metadata in Rust
Learn how to read Cargo package metadata like version, name, and authors in Rust using compile-time environment macros.
Default Function Arguments in Rust: What to Use Instead
Learn how Rust handles default function arguments, why they are not supported, and practical patterns to achieve similar behavior.
Fixing Rust "linker 'cc' not found" on Debian in WSL
Learn why Rust shows "linker 'cc' not found" on Debian in WSL and how to fix it by installing the required C build tools.