-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhashset.rs
62 lines (54 loc) · 1.96 KB
/
hashset.rs
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* In this module we will go through few examples and explain HashSet in rust lang.
* Needed import. (line 14)
* What is HashSet explained. (line 17)
* HashSet example and syntax. (line 22)
* HashSet insert(); example. (line 25)
* HashSet len(); example. (line 33)
* HashSet iter(); example. (line 36)
* HashSet get(); example. (line 41)
* HashSet contains(); example. (line 52)
* HashSet remove(); example. (line 57)
*/
pub mod module {
use std::collections::HashSet; // NOTE: Needs to be imported to use HashMap.
pub fn hashset() {
// HashSet is a set of unique values of type T. Adding and removing values is fast,
// and it is fast to ask whether a given value is in the set or not.
// The HashSet structure is defined in the std::collections module.
// This module should be explicitly imported to access the HashSet structure.
// HashSet example and syntax.
// let mut hash_set_name = HashSet::new();
// HashSet insert();
let mut names = HashSet::new();
names.insert("Joe");
names.insert("Karen");
names.insert("Jack");
names.insert("Joe"); //duplicates not added
println!("{:?}",names);
// HashSet len();
println!("size of the set is {}",names.len());
// HashSet iter();
for name in names.iter() {
println!("{}",name);
}
// HashSet get();
match names.get(&"Jack"){
Some(value)=>{
println!("found {}",value);
}
None =>{
println!("not found");
}
}
println!("{:?}",names);
// HashSet contains();
if names.contains(&"Joe") {
println!("found name");
}
// HashSet remove();
println!("length of the Hashset before remove() : {}",names.len());
names.remove(&"Karen");
println!("length of the Hashset after remove() : {}",names.len());
}
}