rust(47)-key-value哈希-HashMap,BTreeMap,HashSet,BTreeSet

.一、
1、映射hash中键唯一
2、HashMap key可哈希,BTreeMapkey 可排序
3、HashMap无序,BTreeMap有序
二、
1、集合键(元素)唯一
2、HashSet元素可哈希,BTreeSet可排序
3、HashSet无序,BTreeSet有序
4、实质为HashMap<K,()>、BTreeMap<K,()>,键为元素,值为空元组。

    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `F:\learn\rustlearn\learn48\target\debug\learn48.exe`
{3: 30, 2: 20, 9: 90, 8: 80, 5: 50, 4: 40, 6: 60, 7: 70, 1: 10}
{10: 100, 11: 110, 12: 120, 13: 130, 14: 140, 15: 150, 16: 160, 17: 170, 18: 180, 19: 190}
hamp 包括  8=>Some(80)
bamp 包括  18=>Some(180)
{"hset1", "hset2"}
{"bset1", "bset2"}


------------------
(program exited with code: 0)

请按任意键继续. . .
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::BTreeSet;
fn main() {
	let mut hmap=HashMap::new();
	let mut bmap=BTreeMap::new();	
	for i in 1..10{
	    hmap.insert(i,i*10);		
	}
	for i in 10..20{
	    bmap.insert(i,i*10);	
	}
    println!("{:?}",hmap); 
    println!("{:?}",bmap); 
    if hmap.contains_key(&8){
		println!("hamp 包括  {}=>{:?}",8,hmap.get(&8));
	}
    if hmap.contains_key(&18){
		println!("hamp 包括  {}=>{:?}",18,bmap.get(&18));
	}	
    if bmap.contains_key(&8){
		println!("bamp 包括  {}=>{:?}",8,hmap.get(&8));
	}
    if bmap.contains_key(&18){
		println!("bamp 包括  {}=>{:?}",18,bmap.get(&18));
	}	
	let mut bset=BTreeSet::new();		
	let mut hset=HashSet::new();
	hset.insert("hset1");
	hset.insert("hset2");		
	bset.insert("bset1");
	bset.insert("bset2");	
    println!("{:?}",hset); 
    println!("{:?}",bset); 		
}
发布了448 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/104999488