zoukankan      html  css  js  c++  java
  • 3.3 rust HashMap

     The type HashMap<K, V> stores a mapping of keys of type K to values of type V. It does this via a hashing function, which determines how it places these keys and values into memory.

    use std::collections::HashMap;
    
    
    pub fn add1(){
        let mut scores = HashMap::new();
        scores.insert(String::from("Blue"),10);
        scores.insert(String::from("Yellow"),50);
    
    }

    Just like vectors, hash maps store their data on the heap. This HashMap has keys of type String and values of type i32. Like vectors, hash maps are homogeneous: all of the keys must have the same type, and all of the values must have the same type.

    pub fn get_map(){
        let teams = vec![String::from("Blue"), String::from("Yellow")];
        let initial_scores = vec![10, 50];
    
        let _scores: HashMap<_, _> =
            teams.into_iter().zip(initial_scores.into_iter()).collect();
    }

    Hash Maps and Ownership

    简单类型复制,复合类型移动并具有ownership关系,引用类型除外

    fn main() {
        use std::collections::HashMap;
    
        let field_name = String::from("Favorite color");
        let field_value = String::from("Blue");
    
        let mut map = HashMap::new();
        map.insert(field_name, field_value);
        // field_name and field_value are invalid at this point, try using them and
        // see what compiler error you get!
    }

    We aren’t able to use the variables field_name and field_value after they’ve been moved into the hash map with the call to insert.

    If we insert references to values into the hash map, the values won’t be moved into the hash map. The values that the references point to must be valid for at least as long as the hash map is valid.

    Accessing Values in a Hash Map

    pub fn m1(){
        let mut scores = HashMap::new();
        scores.insert(String::from("Blue"),10);
        scores.insert(String::from("Yellow"),50);
    
        let team_name = String::from("Blue");
        let score = scores.get(&team_name);
    
        println!("{:?}",score);
    
        println!("--------------------------");
    
    
        for (key, value) in &scores {
            let val = value + 1;
            println!("{}: {}", key, val);
        }
    }

    Here, score will have the value that’s associated with the Blue team, and the result will be Some(&10). The result is wrapped in Some because get returns an Option<&V>; if there’s no value for that key in the hash map, get will return None

    $ cargo run
       Compiling a_map v0.1.0 (/opt/wks/rust/a_map)
        Finished dev [unoptimized + debuginfo] target(s) in 0.28s
         Running `target/debug/a_map`
    ------------------
    Some(10)
    --------------------------
    Yellow: 51
    Blue: 11

    Updating a Hash Map

    //存在就覆盖
    pub fn m2(){
        let mut scores = HashMap::new();
        scores.insert(String::from("Blue"),10);
        scores.insert(String::from("Blue"),50);
    
        let team_name = String::from("Blue");
        let score = scores.get(&team_name);
    
        println!("{:?}",score);
    }
    Some(50)
    //存在就跳过
    pub fn m3(){
        let mut scores = HashMap::new();
        scores.insert(String::from("Blue"),10);
        scores.entry(String::from("Blue")).or_insert(50);
    
        let team_name = String::from("Blue");
        let score = scores.get(&team_name);
    
        println!("{:?}",score);
    }
    Some(10)
  • 相关阅读:
    mysql数据库(1)
    通过全局异常处理机制实现接口参数校验返回指定返回类型
    http接口安全校验
    java 锁机制介绍
    通过反射获取类的所有属性值拼接成字符串工具类
    Mybatis中出现java.sql.SQLException: 无效的列类型: 1111
    判断两个Long相等
    jwt工具类
    mybatis #{}和${}的区别是什么
    报错解决NoSuchMethod。。。
  • 原文地址:https://www.cnblogs.com/perfei/p/13559006.html
Copyright © 2011-2022 走看看