stringindexingrustcharacter

How can I find the index of a character in a string in Rust?


I have a string with the value "Program", and I want to find the index of character 'g' in that string.


Solution

  • You are looking for the find method for String. To find the index of 'g' in "Program" you can do

    "Program".find('g')
    

    Docs on find.