Home
Search
Rust Find String Examples (IndexOf)Search for patterns in strings with the find and rfind functions. Use find as a string IndexOf.
Find strings. Often we wish to locate the index of a substring inside another string. In Rust, we have no IndexOf function but we have find().
Function details. Find() searches from the start of a string. And rfind begins at the rightmost position. A function like is_ascii_digit can be used to match character groups.
Find example. To start, we use the find() function in its simplest way. Find() works in the same way as other languages like Python. But we must unwrap the result.
Part A We use find() and get an option—this helps us determine if a value exists or not. To get the actual value, we use if let Some.
if
Part B After we call find() successfully we can use get() to take a substring at the index returned.
IndexOf The find() function can work exactly like IndexOf in other languages. It returns None if no match is found, not -1.
Rust program that find substring in string
fn main() { // Part A: use find as indexof. let value = "_xy_xy"; if let Some(result) = value.find("xy") { println!("INDEXOF: {result}"); } // Part B: get substring based on index. let animals = "cat and dog"; if let Some(result) = animals.find("do") { if let Some(inner) = animals.get(result..) { println!("SUBSTRING AT DO: {inner}"); } } }
INDEXOF: 1 SUBSTRING AT DO: dog
No result. What is the best way to handle no result from find() and rfind? The version 2 in this example is probably the best solution—we can even call find() directly in the if-statement.
Version 1 It is possible to test the result directly against None, but we still need to unwrap() the result in this case.
Version 2 We can test the result of find() in an if-statement, and use Some() to unwrap its result if it is found.
Rust program that handles no match from find
fn main() { let value = "aa"; // Version 1: if no result is found, find returns None. let result = value.find("zz"); if result == None { println!("Returned None"); } // Version 2: use Some to test, and then get the actual result. let result2 = value.find("a"); if let Some(m) = &result2 { println!("{}", m); } }
Returned None 0
Rfind example. This function does the same thing as find but proceeds from the rightmost position towards the left. It acts in reverse.
And Sometimes we have a string like the one in the example that contains two "xy" substrings.
Rfind With the example string, rfind will find the second instance, while find will return the first.
Rust program that uses rfind
fn main() { let value = "_xy_xy"; // Use rfind to search from the right. let result = value.rfind("xy").unwrap(); println!("RFIND: {}", result); }
RFIND: 4
First digit. We can pass a function or closure to the find() function. Suppose we want to find the first digit in a string, and take the remaining part as a substring.
Substring
Here We pass is_ascii_digit to find. Then we call get() to extract a substring based on the first digit's position.
Rust program that uses function as argument to find
fn main() { let value = "abc123"; // Find index of first digit. let result = value.find(|c: char| c.is_ascii_digit()).unwrap(); // Take substring of remaining chars. let numbers = value.get(result..).unwrap(); // Print results. println!("FIRST DIGIT: {}", result); println!("FIRST DIGIT START: {}", numbers); }
FIRST DIGIT: 3 FIRST DIGIT START: 123
A review. We search strings with the find function, which is the same thing as an IndexOf function. We can search from start to end, and in the opposite direction.
starts with
Home
© 2007-2022 sam allen.
see site info on the changelog.