Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to merge dictionaries in Rust

2 Answers

0 votes
use std::collections::HashMap;

fn main() {
    let mut dict1 = HashMap::new();
    dict1.insert("rust", 4);
    dict1.insert("c", 6);
    dict1.insert("dart", 3);

    let mut dict2 = HashMap::new();
    dict2.insert("java", 8);
    dict2.insert("dart", 9);
    dict2.insert("nodejs", 7);
    dict2.insert("c++", 2);

    dict1.extend(&dict2);

    println!("{:#?}", dict1);
}




/*
run:

{
    "c++": 2,
    "rust": 4,
    "c": 6,
    "java": 8,
    "dart": 9,
    "nodejs": 7,
}

*/

 



answered Dec 15, 2022 by avibootz
edited Dec 15, 2022 by avibootz
0 votes
use std::collections::HashMap;

fn main() {
    let mut dict1 = HashMap::new();
    dict1.insert("rust", 4);
    dict1.insert("c", 6);
    dict1.insert("dart", 3);

    let mut dict2 = HashMap::new();
    dict2.insert("java", 8);
    dict2.insert("dart", 9);
    dict2.insert("nodejs", 7);
    dict2.insert("c++", 2);
    
    let mut merge = HashMap::new();
    merge.extend(dict1);
    merge.extend(dict2);
    
    println!("{:#?}", merge);
}




/*
run:

{
    "dart": 9,
    "c": 6,
    "nodejs": 7,
    "c++": 2,
    "rust": 4,
    "java": 8,
}

*/

 



answered Dec 15, 2022 by avibootz
...