-
Notifications
You must be signed in to change notification settings - Fork 0
Rust:hash_map
springroll edited this page Dec 14, 2019
·
1 revision
連想配列HashMapの使い方
- インポートと宣言
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);