Skip to content

Rust:hash_map

springroll edited this page Dec 14, 2019 · 1 revision

HashMap

Overview

連想配列HashMapの使い方

Code

  • インポートと宣言
use std::collections::HashMap;

let mut map = HashMap::new();
  • カウント
let text = "hello world wonderful world";

for word in text.split_whitespace() {
    // keyに対応するentryを取得する。ない場合は0をinsert
    let count = map.entry(word).or_insert(0);
    *count += 1;  // countは可変参照なので、参照外しをしてインクリメントする
}

println!("{:?}", map);

Detail

Clone this wiki locally