get last index of string rust
To get the last index of a string in Rust, you can use the rfind
method. Here's an example:
fn main() {
let my_string = String::from("Hello, world!");
let last_index = my_string.rfind("o");
println!("Last index: {:?}", last_index);
}
In this example, the rfind
method is called on the my_string
variable, passing the substring "o" as an argument. The method returns an Option<usize>
, which represents the index of the last occurrence of the substring in the string. If the substring is found, the index is returned as Some(index)
, otherwise, None
is returned.
Please note that the rfind
method returns the byte index, not the character index. If you're working with Unicode characters, you may need to handle them differently.