find the last character in a string in rust

To find the last character in a Rust string, you can use the .chars() method to create an iterator over the string's characters, and then use the .last() method to retrieve the last character.

main.rs
let my_string = String::from("hello world");
let last_char = my_string.chars().last();
87 chars
3 lines

Alternatively, you can use the .pop() method to remove and return the last character of the string, like so:

main.rs
let mut my_string = String::from("hello world");
let last_char = my_string.pop();
82 chars
3 lines

related categories

gistlibby LogSnag