String Methods
Manipulating text.
String Methods is a free Learn Rust Coding lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Working with Text
Rust's str type offers many built-in methods for inspecting and transforming text. These work on both String and &str.
fn main() {
let s = "Hello, Rust";
println!("length {}", s.len());
}Changing Case
Use to_uppercase and to_lowercase to change letter case. They return a new String.
fn main() {
let s = "Rust";
println!("{}", s.to_uppercase());
println!("{}", s.to_lowercase());
}Trimming Whitespace
trim removes leading and trailing whitespace, returning a slice into the original text.
fn main() {
let padded = " spaced ";
let clean = padded.trim();
println!("[{}]", clean);
}Checking Content
Test text with contains, starts_with, and ends_with. Each returns a bool.
fn main() {
let s = "rust_lang.txt";
println!("{}", s.contains("lang"));
println!("{}", s.ends_with(".txt"));
}Replacing Text
replace swaps every occurrence of one substring with another, producing a new String.
fn main() {
let s = "I like cats. Cats are nice";
let dogs = s.replace("ats", "ats and dogs");
println!("{}", dogs.replace("cats", "pets"));
}Splitting Text
split breaks text on a delimiter and yields an iterator. split_whitespace splits on any whitespace.
fn main() {
let csv = "a,b,c";
for part in csv.split(',') {
println!("{}", part);
}
}Splitting into a Vec
Collect the split pieces into a vector when you need to store them.
fn main() {
let line = "one two three";
let words: Vec<&str> = line.split_whitespace().collect();
println!("{:?} ({} words)", words, words.len());
}Finding a Substring
find returns the byte index of the first match as an Option, or None if absent.
fn main() {
let s = "abcdef";
match s.find('d') {
Some(i) => println!("found at {}", i),
None => println!("not found"),
}
}Iterating Characters
The chars method yields each character. Use it to process text one char at a time.
fn main() {
let s = "abc";
for c in s.chars() {
println!("{}", c);
}
}Counting Characters
To count characters (not bytes), use chars().count(). This matters for non-ASCII text.
fn main() {
let s = "hello";
println!("{} chars", s.chars().count());
}Chaining Methods
String methods return new values, so you can chain them into a clean transformation.
fn main() {
let input = " Hello World ";
let result = input.trim().to_lowercase().replace(' ', "_");
println!("{}", result);
}Quick Check
Recall which method removes surrounding whitespace.
Recap
Common string methods:
- Case:
to_uppercase,to_lowercase. - Cleanup:
trim,replace. - Checks:
contains,starts_with,find. - Breaking up:
split,split_whitespace,chars.
Most return new values, so they chain nicely.
fn main() {
let raw = " Rust,Go,Lua ";
let langs: Vec<&str> = raw.trim().split(',').collect();
println!("{:?}", langs);
}Frequently asked questions
Is the “String Methods” lesson free?
Yes — the full text of “String Methods” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.
What will I learn in “String Methods”?
Manipulating text. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Learn Rust Coding?
No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “String Methods” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Learn Rust Coding lesson?
Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- String vs &str
- Slices
- String Methods
- UTF-8 and Chars