find the first index of a substring in a string in rust

main.rs
fn main() {
    let my_string = String::from("Welcome to Rust Programming");
    let my_substring = "Rust";
    
    if let Some(i) = my_string.find(my_substring) {
        println!("Index of first substring: {}", i); // 11
    } else {
        println!("Substring not found");
    }
}
286 chars
11 lines

The find() function returns the first index of the substring if found, and Noneif not found. It is used together with pattern matching in this case to determine whether the substring is found or not, and retrieve the index of the first occurrence if it exists.

gistlibby LogSnag