-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashmaps.rs
More file actions
31 lines (25 loc) · 1.01 KB
/
Copy pathhashmaps.rs
File metadata and controls
31 lines (25 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//Hashmaps
use std::collections::HashMap;
// fn main(){
// let mut hm: HashMap<String,u32> = HashMap::new();
// hm.insert(String::from("deekshant"),20);
// hm.insert(String::from("Harkirat"),22);
// let first_user_age = hm.get("deekshant");
// match first_user_age {
// Some(age) => println!("age is {}",age),
// None => println!("Key was not found in the hashmap"),
// }
// }
//Create a function that takes a vector of a tuple containing String and number, and returns a hashmap with key as the string and value as the number of the hashmap.
fn get_values_from_vector(vec: Vec<(String,i32)>) -> HashMap<String,i32>{
let mut hm: HashMap<String,i32> = HashMap::new();
for (key,value) in vec{
hm.insert(key,value);
}
return hm;
}
fn main(){
let vec: Vec<(String,i32)> = vec![(String::from("Deekshant"),20),(String::from("Harkirat"),22)];
let ans = get_values_from_vector(vec);
println!("the hashmap with the values of vector of a tuple: {:?}",ans);
}