LoginSignup
0
0

More than 1 year has passed since last update.

rust data struct説明--HashMap

Posted at

rust data struct説明--HashMap

New HashMap

 std::collections::HashMap使用方法

1create new hashmap

use std::collections::HashMap
let mut scores = hashmap::new()

2 insert value into hashmap

scores.insert(key,value)

3 get value

onwerkey = String::from(key)
scores.get(&onwerkey)

4 Hash Mapの完全使用方法

use std::collections::HashMap;


//#[derive(Hash)] attribute is used to automatically generate a Hash implementation for a struct or an enum.
#[derive(Hash)]
struct Persion {
    first_name: string,
    last_name: String,
}

impl Person {
    fn new(first: &str, last: &str) -> Self {
        Person {
            first_name; first.to_string(),
            last_name: last.to_string(),
        }
    }
}

fn main() {
    let mut book = HashMap::new();
    book.insert(Person::new("John","smith","521-8976"));
    book.insert(Person::new("sandra","dee","521-9655"));
    book.insert(Person::new("Ted","smith","418-4165"));

    let P = Person::new("John","Smith");
    if let Some(phone) = book.get(&p) {
        //find the book that person haved
        println!("Phone number found: {}", phone);
    }
    //delete recode
    book.remove(&p)

    println!("Find Key:{}",book.contains_key(&p));




}

5 entry Apiの使用と基本使用の違う

// 基本使用,2回操作、1キーの確認2キーを増加
if map.contains_key(key) {
    map.insert(key,value);
}

//entry apiの使用
map.entry(key).or_insert(value)

6 hashmap中にキーの変更がダメです

use std::hash::{Hash, Hasher};
use std::collections::HashMap;
use std::cell::Cell;

#[derive(Eq, PartialEq)]
struct BadKey {
    value: Cell<i32>
}

impl BadKey {
    fn new(v: i32) -> Self {
        BadKey { value: Cell::new(v)}
    }
}

impl Hash for BadKey {
    fn hash<H: Hasher>(&self, state: &mut H){
        self.value.get().hash(state);
    }
}
fn main() {
    let mut map = HashMap::new();
    map.insert(BadKey::new(1),100);
    map.insert(BadKey::new(2),200);
    for key in map.keys() {
        key.value.set(key.value.get() * 2);
    }
    println!("Find key 1:{:?}",map.get(&BadKey::new(1)));
    println!("Find key 2:{:?}",map.get(&BadKey::new(2)));
    println!("Find key 4:{:?}",map.get(&BadKey::new(4)));

}
//response
//Find key 1:None
//Find key 2:None
//Find key 4:None

結果;変更のキーと変更しないのキーは探さない、その原因でkeyはdynamic keyはダメです

7 存在のリストを付いて、Hash Mapを作成します

                                                                                                                                                 

use std::collections::HashMap;
let solar_distance = HashMap::from([
    ("Mercury", 0.4),
    ("Venus", 0.7),
    ("Earth", 1.0),
    ("Mars", 1.5),
]);

8別の方法

use std::collections::HashMap;
let a  = HashMap::new()

a.is_empty()
a.len()
//
a.drain().drain()(.take(1)
a.keys()
a.containers_key()
a.remove(&mut value) --> Removes a key from the map, returning the value at the key if the key was previously in the map.
a.remove_entry(&Mut value) -->Removes a key from the map, returning the stored key and value if the key was previously in the map.


0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0