Rust Collections: DSL Operations on Sequences
Tags: rust
Published: 2020-10-06 13:31:12

In this blog post, we'll explore different Domain Specific Languages (DSL) operations that can be applied on Rust Collections-Sequences.

In case, if you would like to learn more about DSL, I strongly suggest you to check out [this](https://martinfowler.com/books/dsl.html) excellent write up from Martin Fowler.

Following are different Rust collections available in Standard library and it can be grouped into following categories:

+ Sequences: Vec, VecDeque, LinkedList
+ Maps: HashMap, BTreeMap
+ Sets: HashSet, BTreeSet
+ Misc: BinaryHeap

##### Vec

A continuous growable collection of elements. For more details, please check [this] (https://doc.rust-lang.org/std/vec/struct.Vec.html) page from rust documentation.

Vector is also of LIFO (Last In First Out) or Stack typed data structure.

Some of the useful DSL operations that can be performed on Vector can be found below:

```rust
    // declare new vector
    let i = vec![1, 2, 3, 4, 5];
    println!("{:?}", i);

    output: [1, 2, 3, 4, 5]

    // apply some transformation on each element of vector
    i.iter().map(|x| x*2).for_each(|y| print!("{}, ", y));

    output: 2, 4, 6, 8, 10,

    // find sum of all elements in a vector
    let s1: i32 = i.iter().sum();
    println!("{}", s1);

    output: 15

    // find product of all elements in a vector
    let s2: i32 = i.iter().product();
    println!("{}", s2);

    output: 120

    // Merge 2 different vector and create Tuple object 
    let j = vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
    let t = i.iter().zip(j.iter());
    t.for_each(|x| print!("{:?} ", x));

    output: (1, 10) (2, 9) (3, 8) (4, 7) (5, 6) 

    // Create HashMap from a Vector
    // Following operation will create a HashMap with each Vector element as Key and len as Value
    let s3 = vec!["Siva", "Kumar"];    
    let hm1: HashMap<_, _> = s3.iter().map(|x| (x, x.len())).collect();
    println!("{:?}", hm1);

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

    // Create HashSet from a Vector
    let hs1: HashSet<_> = vec![1, 1, 2, 2, 4, 4, 5, 5, 6, 6].into_iter().collect();
    println!("{:?}", hs1);

    output: {2, 4, 6, 5, 1}   
```

##### VecDeque

A double-ended queue implemented with a growable ring buffer. For more details, please check [this] (https://doc.rust-lang.org/std/collections/struct.VecDeque.html) page from rust documentation.

Find below some useful DSL operations that can be performed on VecDeque.

```rust
    // VecDeque can be created from Vector as below
    let i: VecDeque<_> = vec![1, 2, 3, 4, 5].into_iter().collect();
    println!("{:?}", i);

    // VecDeque can be created as below as well
    let mut i: VecDeque<i32> = VecDeque::new();
    i.push_back(1);
    i.push_back(2);
    i.push_back(3);
    i.push_back(4);
    i.push_back(5);
    println!("{:?}", i);

    // apply some transformation on each element of VecDeque
    i.iter().map(|x| x*2).for_each(|y| print!("{}, ", y));

    // find sum of all elements in a VecDeque
    let s1: i32 = i.iter().sum();
    println!("{}", s1);
    
    // find product of all elements in a VecDeque
    let s2: i32 = i.iter().product();
    println!("{}", s2);
   
    // Merge 2 different VecDeque and create Tuple object 
    let j: VecDeque<_> = vec![10, 9, 8, 7, 6, 5, 4, 3, 2, 1].into_iter().collect();
    let t = i.iter().zip(j.iter());
    t.for_each(|x| print!("{:?} ", x));
    
    // Create HashMap out from a Vector object
    // Following operation will create a HashMap with each Vector element as Key and len as Value
    let s3: VecDeque<_> = vec!["Siva", "Kumar"].into_iter().collect();    
    let hm1: HashMap<_, _> = s3.iter().map(|x| (x, x.len())).collect();
    println!("{:?}", hm1);
```

##### LinkedList

LinkedList permits users to push and pop elements from either end in constant time. For more details, please check [this] (https://doc.rust-lang.org/std/collections/struct.LinkedList.html) page from rust documentation.

Some of the useful DSL operations on LinkedList can be found here

```rust
    // LinkedList can be created from Vector as below
    let ll: LinkedList<_> = vec![1, 2, 3, 4, 5].into_iter().collect();
    println!("{:?}", ll);
    
    // LinkedList can be created as below as well
    let mut i: LinkedList<i32> = LinkedList::new();
    i.push_front(1);
    i.push_front(2);
    i.push_front(3);
    i.push_front(4);
    i.push_front(5);
    println!("{:?}", i);
    
    // apply some transformation on each element of LinkedList
    i.iter().map(|x| x*2).for_each(|y| print!("{}, ", y));
    
    // find sum of all elements in a LinkedList
    let s1: i32 = i.iter().sum();
    println!("{}", s1);
    
    // find product of all elements in a LinkedList
    let s2: i32 = i.iter().product();
    println!("{}", s2);
    
    // Create HashMap out from a Vector LinkedList
    // Following operation will create a HashMap with each LinkedList element as Key and len as Value
    let s3: LinkedList<_> = vec!["Siva", "Kumar", "Siva"].into_iter().collect();    
    let hm1: HashMap<_, _> = s3.iter().map(|x| (x, x.len())).collect();
    println!("{:?}", hm1);

    // Create HashSet from a LinkedList
    let s4: LinkedList<_> = vec![1, 1, 2, 2, 4, 4, 5, 5, 6, 6].into_iter().collect();
    let hs1: HashSet<_> = s4.iter().collect();
    println!("{:?}", hs1);
```

Please feel free to share your feedback.

Happy reading!!!