Chat app: Struct vs HashMap
Tags: rust, beginners
Published: 2022-09-03 09:00:36

#Quick look at Struct and HashMap using сhat app as an example

First of all, I want to thank these people: [Aleksandr Petrosyan](https://github.com/appetrosyan), [Victor Gridnevsky](https://github.com/6r1d/) and [Nikita Pusankov](https://dev.to/humb1t).

Sometimes a comparison of different methods can be useful for studying. In this case I mean [Structures](https://doc.rust-lang.org/book/ch05-00-structs.html) and [Hash Map](https://doc.rust-lang.org/book/ch08-03-hash-maps.html). Structure is a data type. Hash Map is a collection. Both are suitable for transmitting a user name and message.
Example of struct and Hash Map:

```Rust
use std::collections::HashMap;

struct Some_Struct { dictionary: HashMap<char, String> }

fn main() {
    // variable of type HashMap<char, String>
    let a: HashMap<char, String> = HashMap::new();
    // variable of type Some_Struct { dictionary: HashMap<char, String> }
    let b: Some_Struct = Some_Struct { dictionary: a };
}
```

Here is an example of a terminal chat application made with [Structures](https://github.com/antonovmike/chat/tree/structures_0) and [Hash Map](https://github.com/antonovmike/chat/tree/hashmap_0). Here I will show you just a couple of things. All the code is in the repository.

Cargo.toml dependencies:
```Rust
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.67"
colored = "2.0.0"
```

**transmitter.rs**
In the first case, you need to create a data type ["structure"](https://github.com/antonovmike/chat/blob/structures_0/chat_user/src/transmitter.rs) and create a variable of type "structure" for storing user’s name and message.
In the second case, we create a variable of the [Hash Map](https://github.com/antonovmike/chat/blob/hashmap_0/chat_user/src/transmitter.rs) type and also insert some data. The key will be the user name, the value will be the message.

**receiver.rs**
The same, but we add id as a field of a new structure and an old structure to the new [structure](https://github.com/antonovmike/chat/blob/structures_0/chat_server/src/receiver.rs).

chat_server/src/structures.rs
```Rust
use std::iter::Map;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug)]
pub struct UserData {
    pub name: String,
    pub message: String,
}

#[derive(Debug)]
pub struct UserID {
    pub id: String,
    pub data: UserData,
}

```
chat_server/src/receiver.rs Structure
```Rust
let deserialized: UserData = serde_json::from_str(&serde_message).expect("Could not read");
```
We don’t have to add user ID addr to [Hash Map](https://github.com/antonovmike/chat/blob/hashmap_0/chat_server/src/receiver.rs), we can use it like this:
```Rust
addr.to_string()
```
chat_server/src/receiver.rs Hash Map
```Rust
let deserialized: HashMap<String, String> = serde_json::from_str(&serde_message).expect("Could not read");
```
I made two versions of the same program for the sole purpose of getting to know better the work of Hash Map, which for some reason I have never used before. I hope this text was helpful to someone.

##A little bit about DATA_SIZE
All data are transmitted as a vector of u8 using serde and serde_json. For a correct deserialization we need a limit. I don't understand exactly how it works yet. Just applying it for now. I think I'll figure it out later. For now, just an example of how to use it:
chat_server and  user
```Rust
const DATA_SIZE: usize = 96;
```
chat_user/src/transmitter.rs
```Rust
buff_serde.resize(DATA_SIZE, 0);
```
chat_server/src/receiver.rs
```Rust
let mut buff_serde = vec![0; DATA_SIZE];
```

##Extra crate
Crate colored = “2.0.0” used as a decoration tool. For example:
```Rust
.bold().yellow()
.italic().on_green()
```
##Useful methods
[resize()](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.resize) resizes the Vec in-place so that len is equal to new_len.

[into_iter()](https://doc.rust-lang.org/std/iter/index.html) iterates over T.

[take_whale()](https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.take_while) takes a closure as an argument. It will call this closure on each element of the iterator, and yield elements while it returns true.

[read_exact()](https://doc.rust-lang.org/std/io/trait.Read.html#method.read_exact) reads the exact number of bytes required to fill buf.

[read_line()](https://doc.rust-lang.org/std/io/trait.BufRead.html#method.read_line) reads bytes from the underlying stream until the newline delimiter is found.