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

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