• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

3.3rustHashMap

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 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)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab 打印不要科学计数法发布时间:2022-07-18
下一篇:
基于MATLAB GUI 双音多频拨号音编解码系统发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap