Rust Collections: DSL Operations on Sets
Tags: rust
Published: 2020-10-06 14:42:10

In this post, we'll explore different DSL operations that can be carried on Sets collections (HashSet & BTreeSet). In case, if you've not read the [1st part] (https://dev.to/ssivakumar77/rust-collections-dsl-operations-on-sequences-2386) & [2nd part] (https://dev.to/ssivakumar77/rust-collections-dsl-operations-on-maps-4m4k) of this on Rust Collections, please do read.

##### HashSet

HashSet is a set of hash based data structure where it doesn't allow to have duplicates. For more details, please check [this] (https://doc.rust-lang.org/std/collections/struct.HashSet.html) page from rust documentation.

```rust
    // Create new HashSet
    let mut hs1: HashSet<_> = HashSet::new();
    hs1.insert("Siva".to_string());
    hs1.insert("Kumar".to_string());
    println!("{:?}", hs1);

    output: {"Siva", "Kumar"}
    
    // HashSet can also be created from an Array like below
    let hs2: HashSet<&str> = ["Siva", "Kumar", "Sivakumar"].iter().cloned().collect();
    println!("{:?}", hs2);

    output: {"Siva", "Sivakumar", "Kumar"}
    
    // Check the total elements in HashSet
    println!("Length: {}", hs2.len());

    output: Length: 3
    
    // Creating Hashset of integers
    let hs3: HashSet<i32> = [2, 4, 5, 6, 2, 8].iter().cloned().collect();
    println!("{:?}", hs3);

    output: {6, 8, 5, 4, 2}
    
    // Perform `sum` actions on the values of HashSet
    println!("Sum of all values in HashSet: {}", hs3.iter().sum::<i32>());

    output: Sum of all values in HashSet: 25
    
    // Perform `product` actions on the values of HashSet
    println!("Product of all values in HashSet: {}", hs3.iter().product::<i32>());

    output: Product of all values in HashSet: 1920
    
    // Another way of performing aggregte operation
    // Perform `fold` actions on the values of HashSet
    // This will multiply each elements of HashSet
    println!("Product of all values in HashSet: {}", hs3.iter().fold(1, |x, y| x*y));

    output: Product of all values in HashSet: 1920
    
    // Create Vector object from HashSet
    // In this example, we use values of HashSet to create new Vector
    let v1: Vec<_> = hs2.iter().collect();
    println!("{:?}", v1);

    output: ["Siva", "Sivakumar", "Kumar"]
    
    // Create new HashMap object from HashSet
    // In this example, we use each element of HashSet as key and its length as value
    let s1: HashMap<_, _> = hs2.iter().map(|x| (x, x.len())).collect();
    println!("{:?}", s1);

    output: {"Kumar": 5, "Sivakumar": 9, "Siva": 4}

    
    // Apply some filter operations on HashSet
    let v2: Vec<_> = hs2.iter().filter(|e| e.len() >= 4).map(|e| e.len()).collect();
    println!("{:?}", v2);

    output: [4, 9, 5]
```
##### BTreeSet

A Set based on Binary Tree. For more details, please check [this] (https://doc.rust-lang.org/std/collections/struct.BTreeSet.html) page from rust documentation.

```rust
    // Create new BTreeSet
    let mut bt1: BTreeSet<_> = BTreeSet::new();
    bt1.insert("Siva".to_string());
    bt1.insert("Kumar".to_string());
    println!("{:?}", bt1);
    
    // BTreeSet can also be created from an Array like below
    let bt2: BTreeSet<&str> = ["Siva", "Kumar", "Sivakumar"].iter().cloned().collect();
    println!("{:?}", bt2);
    
    // Check the total elements in BTreeSet
    println!("Length: {}", bt2.len());
    
    // Creating BTreeSet of integers
    let bt3: BTreeSet<i32> = [2, 4, 5, 6, 2, 8].iter().cloned().collect();
    println!("{:?}", bt3);
    
    // Perform `sum` actions on the values of BTreeSet
    println!("Sum of all values in BTreeSet: {}", bt3.iter().sum::<i32>());
    
    // Perform `product` actions on the values of BTreeSet
    println!("Product of all values in BTreeSet: {}", bt3.iter().product::<i32>());
    
    // Another way of performing aggregte operation
    // Perform `fold` actions on the values of BTreeSet
    // This will multiply each elements of BTreeSet
    println!("Product of all values in BTreeSet: {}", bt3.iter().fold(1, |x, y| x*y));
    
    // Create Vector object from BTreeSet
    // In this example, we use values of BTreeSet to create new Vector
    let v1: Vec<_> = bt2.iter().collect();
    println!("{:?}", v1);
    
    // Create new HashMap object from BTreeSet
    // In this example, we use each element of BTreeSet as key and its length as value
    let s1: HashMap<_, _> = bt2.iter().map(|x| (x, x.len())).collect();
    println!("{:?}", s1);
    
    // Apply some filter operations on BTreeSet
    let v2: Vec<_> = bt2.iter().filter(|e| e.len() >= 4).map(|e| e.len()).collect();
    println!("{:?}", v2);
```

Please feel free to share your feedback.

Happy reading!!!