Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Rust String Methods


Share

String Methods

In the Strings chapter, you learned to create strings, add text with push_str(), and get the length with len().

Rust has many more built-in methods. You use them to change, search and clean up text, for example to remove extra spaces from a name that a user typed.


Uppercase and Lowercase

The to_uppercase() and to_lowercase() methods return a new string with all letters changed:

Example

let text = String::from("Hello World");
println!("{}", text.to_uppercase());
println!("{}", text.to_lowercase());
Try it Yourself »

Note: These methods do not change the original string. They return a new String with the result.


Remove Spaces

The trim() method removes spaces at the beginning and the end of a string. The square brackets in this example only show where the text starts and ends:

Example

let name = " Liam ";
println!("[{}]", name);
println!("[{}]", name.trim());
Try it Yourself »

Search in a String

Use contains() to check if a string has a piece of text inside it, starts_with() to check how it begins, and ends_with() to check how it ends. They all return true or false:

Example

let text = String::from("I love Rust");
println!("{}", text.contains("Rust"));
println!("{}", text.starts_with("I"));
println!("{}", text.ends_with("Python"));
Try it Yourself »


Replace Text

The replace() method replaces every match of a text with another text:

Example

let text = String::from("I like cats");
let new_text = text.replace("cats", "dogs");
println!("{}", new_text);
Try it Yourself »

Length in Bytes and Characters

The len() method counts bytes, not characters. For English letters, this is the same, because each letter is one byte. But some characters, like é, use two bytes. To count the characters, use chars().count():

Example

let word = "Café";
println!("{}", word.len());
println!("{}", word.chars().count());
Try it Yourself »

Loop Through Characters

The chars() method gives you the characters of a string one by one, so you can use it in a for loop:

Example

let name = "John";
for c in name.chars() {
  println!("{}", c);
}
Try it Yourself »

Split a String

The split() method cuts a string into parts at a separator, like a comma:

Example

let fruits = "apple,banana,cherry";
for fruit in fruits.split(",") {
  println!("{}", fruit);
}
Try it Yourself »

Common String Methods

MethodDescription
to_uppercase()Returns the string in uppercase letters
to_lowercase()Returns the string in lowercase letters
trim()Removes spaces at the beginning and the end
trim_start()Removes spaces at the beginning
trim_end()Removes spaces at the end
contains(text)Returns true if the string contains the text
starts_with(text)Returns true if the string begins with the text
ends_with(text)Returns true if the string ends with the text
replace(from, to)Replaces every match of one text with another text
len()Returns the length of the string in bytes
is_empty()Returns true if the string has no characters
chars()Gives the characters of the string one by one
split(separator)Splits the string into parts at the separator
split_whitespace()Splits the string into words
repeat(n)Returns the string repeated n times


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.