■ HashMap 구조체를 사용하는 방법을 보여준다.
▶ 예제 코드 (RS)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
use std::collections::HashMap; const VALUE_STRING: &str = "C,C,A,A,A,B,C,C,B,B,B,C,B,C,B,A,C,C,B,C,C,C"; fn main() { let mut hashmap : HashMap<&str, i32> = HashMap::new(); hashMap.insert("A", 0); hashMap.insert("B", 0); hashMap.insert("C", 0); for value in VALUE_STRING.split(',') { hashmap.insert(value, hashMap[value] + 1); } for key in ["A","B","C"] { println!("{} : {:>2}", key, hashmap[key]); } } /* A : 4 B : 7 C : 11 */ |