Rust char_indicesで文字列を可変の配列に変換するサンプル

環境
Windows 10 Home 64bit
rustc 1.66.0

構文
“対象文字列".char_indices().collect::<Vec<_>>();
「char_indices().collect::<Vec<_>>()」を使用して文字列をベクタ(可変の配列)に変換します。

使用例

fn main() {

    let mut str: String = "study".to_string();

    println!( "{:?}", str.char_indices().collect::<Vec<_>>() );

    str = "テストデータ".to_string();

    println!( "{:?}", str.char_indices().collect::<Vec<_>>() );

}

実行結果
[(0, 's’), (1, 't’), (2, 'u’), (3, 'd’), (4, 'y’)]
[(0, 'テ’), (3, 'ス’), (6, 'ト’), (9, 'デ’), (12, 'ー’), (15, 'タ’)]

Rust

Posted by arkgame