I need a data structure similar to HashMap<K, Vec<V>>. What libraries are available

I need a data structure similar to HashMap<K, Vec>. What library is available? I need to put it into the public scope and use it as a pool

Lazy_ Static! {
Pub static ref SERVER_ Table: RwLock<HashMap:<String, Vec<RegistryRam>>=RwLock:: new (HashMap:: new());
}

This way, when I obtain the VEC, I cannot modify it

There's no need for a separate library. HashMap<String, Vec<_>> works just fine behind an RwLock.

Can you elaborate? How are you trying to use it? If you obtain a write guard to the RwLock, you should be able to mutate anything behind it. Example.

use std::collections::HashMap;
use poem::Error;
use serde::{Serialize, Deserialize};
use tokio::{sync::RwLock};
use uuid::Uuid;
use vecmap::VecMap;
lazy_static! {
    ///服务表注册表
    pub static ref SERVER_TABLE: RwLock<HashMap::<String,Vec<RegistryRam>>> = RwLock::new(HashMap::new()); 
}
#[derive(Debug, Clone,Deserialize)]
pub struct RegistryParam {
    pub server_name: String,
    pub ip: String,
    pub port:String,
}

#[derive(Debug,Clone,Serialize)]
pub struct RegistryRam {
    pub id: String,
    pub server_name: String,
    pub ip: String,
    pub port:String,
}




impl RegistryParam {
    pub fn to_registry_ram(&self) -> RegistryRam{
        return RegistryRam {
            id:Uuid::new_v4().to_string(),
            server_name:self.server_name,
            ip:self.ip,
            port:self.port
        }
    }
    pub async fn registry(&self) {
        let mut server_table=SERVER_TABLE.write().await;
        let mut server_tab_vec=server_table.get(&self.server_name);
        match server_tab_vec {
            Some(item) => {
                item.push(self.to_registry_ram())
            },
            None => {},
        }
        // server_table.insert(self.server_name.clone(), self.to_registry_ram())
        
    }
}

This is an example, but compilation exceptions may occur
cannot borrow *item as mutable, as it is behind a & reference
item is a & reference, so the data it refers to cannot be borrowed as mutablerustcClick for full compiler diagnostic

registry_param.rs(42, 18): consider changing this binding's type to be: &mut Vec<RegistryRam>

spider_registry::model::registry_param::RegistryParam

pub fn to_registry_ram(&self) -> RegistryRam

Go to RegistryRam

View Problem (Alt+F8)

No quick fixes available

You didn't show where the error appears. Please, don't paste the output mangled by your IDE; paste the output of cargo check verbatim, including line numbers and span indicators, as a code block (so that its formatting is not lost).

However, I guess that the problem occurs on the line

item.push(self.to_registry_ram())

and that's because you used

server_table.get(&self.server_name)

If you had read the documentation of HashMap, you would have known that .get() always returns an immutable reference to the value, even if you have a mutable reference to the HashMap. You will need to use

server_table.get_mut(&self.server_name)

instead.

1 Like

Thank you very much. I am a newcomer to Trust and I am not familiar with many aspects of Trust