どうも、ちょげ(@chogetarou)です。
文字列(string)の最後の文字を取得する方法を紹介します。
方法

文字列(string)の末尾の文字を取得するには、last()を使います。
まず、文字列からchars()を呼び出します。
chars()からlast()を呼び出します。
そして、last()からunwrap()を呼び出します。
//text=対象の文字列
let last = text.chars().last().unwrap();
上記のlast()は、呼び出した文字列(string)の末尾の文字を取得します。
使用例
fn main() {
let text: &str = "Hello,World";
let last = text.chars().last().unwrap();
println!("{}", last);
}
出力:
d
コメント