Rust学习笔记 5.2 HashMap

5.2 HashMap

  • 散列函数

    • 散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来。该函数将数据打乱混合,重新创建一个叫散列值(hash values, hash codes, hash sums, 或hashes)的指纹
  • HashMap由链表加数组组成,它的底层结构是一个数组,而数组的元素是一个单向链表

  • HashMap 的键可以是布尔型、整型、字符串,或者任意实现了 Eq 和 Hash trait 的其他类型

  • HashMap 也是可增长的,但 HashMap 在占据了多余空间时还可以缩小自己

use std::collections::HashMap;

fn main() {
    
    
    let mut str_map = HashMap::new();
    str_map.insert("star", 18);
    str_map.insert("tears", 19);
    println!("{:#?}", str_map);
    match str_map.get(&"star") {
    
    
        Some(v) => println!("{:#?}", v),
        _ => {
    
    }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_51173321/article/details/126064982
5.2