String::len() / is_empty() / contains()
Rust strings (String and &str) provide many methods for getting the length, checking for emptiness, searching, and testing prefixes or suffixes. Commonly used methods include len(), is_empty(), contains(), starts_with(), and ends_with().
Syntax
let s = String::from("Hello, Rust!");
// Returns the number of bytes (not the number of characters).
let len = s.len(); // 12 (ASCII characters are 1 byte each)
// Returns whether the string is empty.
let empty = s.is_empty(); // false
// Checks whether the string contains a substring.
let has = s.contains("Rust"); // true
// Checks whether the string starts with the given prefix.
let starts = s.starts_with("Hello"); // true
// Checks whether the string ends with the given suffix.
let ends = s.ends_with("!"); // true
// Returns the byte offset of the first match.
let idx = s.find("Rust"); // Some(7)
// Retrieves all match positions.
let positions: Vec<_> = s.match_indices("l").collect();
Method List
| Method | Description |
|---|---|
| s.len() | Returns the number of bytes in the string (not the number of characters). |
| s.is_empty() | Returns whether the string is empty (i.e., len() == 0). |
| s.contains(pattern) | Returns true if the string contains the given pattern. |
| s.starts_with(prefix) | Returns true if the string starts with the given string or character. |
| s.ends_with(suffix) | Returns true if the string ends with the given string or character. |
| s.find(pattern) | Returns the byte offset of the first match as Option<usize>. |
| s.rfind(pattern) | Returns the byte offset of the last match as Option<usize>. |
| s.match_indices(pattern) | Returns an iterator over all match positions and their substrings. |
| s.chars().count() | Returns the number of Unicode characters (handles multibyte characters such as Japanese). |
| s.is_ascii() | Returns whether all characters in the string are ASCII characters. |
Sample Code
fn main() {
let s = String::from("Hello, Rust! Rust is wonderful.");
// Difference between byte length and character count.
println!("len (bytes): {}", s.len());
println!("chars().count() (characters): {}", s.chars().count());
// Checking for empty strings.
let empty = String::new();
let non_empty = String::from("hello");
println!("empty.is_empty(): {}", empty.is_empty());
println!("non_empty.is_empty(): {}", non_empty.is_empty());
println!();
// contains, starts_with, and ends_with.
println!("Contains 'Rust': {}", s.contains("Rust"));
println!("Contains 'Python': {}", s.contains("Python"));
println!("Starts with 'Hello': {}", s.starts_with("Hello"));
println!("Ends with '.': {}", s.ends_with("."));
println!("Ends with 'Rust': {}", s.ends_with("Rust"));
println!();
// find (first position).
match s.find("Rust") {
Some(i) => println!("First position of 'Rust': {}", i),
None => println!("'Rust' not found"),
}
match s.find("Python") {
Some(i) => println!("First position of 'Python': {}", i),
None => println!("'Python' not found"),
}
// rfind (last position).
if let Some(i) = s.rfind("Rust") {
println!("Last position of 'Rust': {}", i);
}
println!();
// match_indices (all matches).
let text = "banana";
let positions: Vec<_> = text.match_indices("an").collect();
println!("Positions of 'an' in 'banana': {:?}", positions);
// ASCII check.
println!("'Hello' is ASCII: {}", "Hello".is_ascii());
println!("'Hello world' is ASCII: {}", "Hello world".is_ascii());
// Practical example: validating user input.
let input = " ";
if input.trim().is_empty() {
println!("\nInput is empty");
}
}
Notes
Rust strings are UTF-8 encoded, and len() returns the number of bytes. When the string contains multibyte characters such as Japanese, the byte length and character count will differ. Use s.chars().count() to get the number of characters.
find() returns a byte offset. This offset is guaranteed to be on a character boundary, so you can use it for slicing like &s[0..idx]. However, slicing at an index that falls in the middle of a multibyte character will cause a panic.
len() returns the number of bytes, not the number of Unicode code points. When you need the character count, always use chars().count().
For string replacement, splitting, and trimming, see String::replace() / split() / trim().
If you find any errors or copyright issues, please contact us.