How to get the index of the last instance of a character in a string with Rust

1 Answer

0 votes
fn main() {
    let s = "Rust is fast and memory-efficient: with no garbage collector";
    let character_to_find = 'o';
    
    if let Some(index) = s.rfind(character_to_find) {
        println!("The last occurrence of '{}' is at index {}", character_to_find, index);
    } else {
        println!("Character '{}' not found in the string", character_to_find);
    }
}



   
/*
run:
  
The last occurrence of 'o' is at index 58
  
*/

 



answered Dec 11, 2024 by avibootz
...