= vec![0, size.try_into().unwrap()];
80 | unsafe {
81 | let ptr = data as *const K as *const u8;
82 | ptr.copy_to_nonoverlapping(bytes.as_mut_ptr(), size);
83 | }
84 |
85 | let byte_sum: usize = bytes.iter().map(|&x| x as usize).sum();
86 |
87 | byte_sum % self.table.capacity()
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/content/hash/hashmap/src/main.rs:
--------------------------------------------------------------------------------
1 | mod hashmap;
2 |
3 | use hashmap::HashMap;
4 | use std::io;
5 |
6 | fn main() -> io::Result<()> {
7 | let mut hm = HashMap::<&str, i32>::new(20);
8 |
9 | hm.push("hello", 10).unwrap();
10 | hm.push("bye", 12).unwrap();
11 |
12 | println!("{:#?}", hm);
13 |
14 | println!("hello: {:#?}", hm.get("hello").unwrap());
15 | println!("bye: {:#?}", hm.get("bye").unwrap());
16 |
17 | Ok(())
18 | }
19 |
20 |
--------------------------------------------------------------------------------
/content/hash/maps.md:
--------------------------------------------------------------------------------
1 | # Hash Maps
2 |
3 | ---
4 |
5 | Are you ready to find out how Hash Tables works?
6 |
7 |
8 | Next -> Hash Tables
9 |
10 |
11 |
12 | Back to main menu
13 |
14 |
--------------------------------------------------------------------------------
/content/hash/tables.md:
--------------------------------------------------------------------------------
1 | # Hash Tables
2 |
3 | Hash tables are data structures that encapsulate a large group of data in a smaller but keyable data structure. Intuitively, think of hash maps as a collection of data that can be indexed based on the data structure you choose to hold it. In this example, we'll be using arrays, but you could use anything that is abstractly contiguous and indexable, as said beforewards.
4 |
5 | ## Abstraction
6 |
7 | Hash tables are in essence, stored by a hash algorithm. Said so, we won't be able to retrieve a data by index like we would with an array. We still could, but since we aren't necessary storing every data contiguously, you may get something completely different in return.
8 |
9 | To make this more simple, we have some data, make it go through a transformation, usually called "Hashing", and them store it based on a value calculated through it's hash value, which is usually a value used by the data structure that holds this hash table, like an index for an array, or a pointer for a linked list.
10 |
11 | ## Hashing
12 |
13 | There are ton of hashing algorithms, but, for sake of simplicity, we'll be using a simple algorithm that sums the binary values of the data being stored, and then "mods" ( % ) the result to the max size of the structure holding it.
14 |
15 | For example, if we try to hash ```"hello"``` inside a hash table with capacity of 20 slots:
16 |
17 | ```rust
18 | const HS_CAP: i32 = 20;
19 | fn main() {
20 | let data: String = String::from("hello");
21 |
22 | let sum: i32 = data
23 | .as_bytes()
24 | .iter()
25 | .sum();
26 |
27 | sum %= HS_CAP;
28 | println!("{}", sum);
29 | }
30 | ```
31 |
32 | ---
33 |
34 | Enjoyed learning about Hash Maps and Hash Tables? Let's find out now what graphs are?
35 |
36 |
37 | Next -> Graph data structures
38 |
39 |
40 |
41 | Back to main menu
42 |
43 |
--------------------------------------------------------------------------------
/content/intro/README.md:
--------------------------------------------------------------------------------
1 | # Introduction
2 |
3 | In this guide we will present the main concepts aimed at studies involving data structures using the Rust programming language as a basis.
4 |
5 | Every topic covered was written with a lot of dedication from all the collaborators, so if you have any modifications that you want to implement, just follow the steps presented on the home page to contribute (creating a fork, modifying and adding your modification with a pull request).
6 |
7 | Written topics explaining certain concepts will be used, in addition to the applicability of each concept with specific codes to be shown and implemented.
8 |
9 | Feel more than welcome to have a little fun in the repository and learn a little more about everything Rust and data structures has to offer!
10 |
11 | ## Why Rust?
12 |
13 | It's cool. Just it. 🦀
14 |
15 | ---
16 |
17 | Ready to start this new adventure?
18 |
19 |
20 | Next -> What are data structures?
21 |
22 |
23 |
24 | Back to main menu
25 |
26 |
--------------------------------------------------------------------------------
/content/intro/typesofdata.md:
--------------------------------------------------------------------------------
1 | # Types of data structures
2 |
3 | Data structures can be differentiated and defined in several types. The types of data to be computed vary according to the programming language to be used, so we will also introduce the data types of the Rust language.
4 |
5 | ## Rust data types
6 |
7 | You can check more detailed documentation by clicking [here](https://doc.rust-lang.org/book/ch03-02-data-types.html).
8 |
9 | ### Integer
10 |
11 | Integers in Rust can have subtypes depending on their uses. Integers are numerical values that do not necessarily have floating points to match their decimal value.
12 |
13 | | Length | Signed | Unsigned |
14 | |---------|--------|----------|
15 | | 8-bit | i8 | u8 |
16 | | 16-bit | i16 | u16 |
17 | | 32-bit | i32 | u32 |
18 | | 64-bit | i64 | u64 |
19 | | 128-bit | i128 | u128 |
20 | | arch | isize | usize |
21 |
22 | ### Floating-Point
23 |
24 | Numbers with values in decimal places, which can be defined in 32-bit or 64-bit sets, with declarations such as f32 and f64 respectively.
25 |
26 | ### Boolean
27 |
28 | It only stores two types of value: true or false.
29 |
30 | ### Character
31 |
32 | Used to store a pure character, being the primitive type that will store any type of data in a simple way.
33 |
34 | ### Other
35 |
36 | There are still types for arrays and tuples, which can be seen in the documentation. Both won't be introduced yet, but be aware that they are also recognized as types.
37 |
38 | ## Data structures
39 |
40 | Now let's see a little about the main types of data structures.
41 |
42 | ### Linear
43 |
44 | We can define the type of linear data structure as the one that will store the data to be accessed by values in indexes and linked lists. An obvious example would be arrays.
45 |
46 | ### Tree
47 |
48 | In this type of data structure we will have a root that will be responsible for enabling all access to the complete data structure through linked nodes. There are several types of trees, such as binary trees and red-black trees.
49 |
50 | ### Hash tables
51 |
52 | The use of a table to map keys with values is a classic feature of hash tables, and may include concepts of separate chaining and linear probing.
53 |
54 | ### Graph
55 |
56 | The applicability of principles on graph theory led to this data structure, using nodes and edges to represent directional flows of networks.
57 |
58 | You can read a little more about graph theory by clicking [here](https://en.wikipedia.org/wiki/Graph_theory).
59 |
60 | ---
61 |
62 | Are you ready to dive a little deeper into the main concepts involving linear data structures?
63 |
64 |
65 | Next -> Linear data structures
66 |
67 |
68 |
69 | Back to main menu
70 |
71 |
--------------------------------------------------------------------------------
/content/intro/whatare.md:
--------------------------------------------------------------------------------
1 | # What are data structures?
2 |
3 | Upon encountering programming, in its first contact it was possible to realize that the computer needs to interact in some way with us, making it necessary to write algorithms in order to make the computer literally compute something.
4 |
5 | Every computation generates a result, which we can store and manipulate in data format. How this data is combined and organized depends on how it will be used for processing. There are several ways to organize these datasets, targeting each specific need.
6 |
7 | The different ways of organizing data for a given task are called data structures, in which each basic structure has a set of operations for data manipulation, including insertion, deletion, search, sorting and ordering of data, making different structures can be organized aiming at a better performance.
8 |
9 | ## Characteristics
10 |
11 | The structures basically can be linear or non-linear, homogeneous or heterogeneous and static or dynamic.
12 |
13 | ### Linear x Non-linear
14 |
15 | The data are stored linearly in linear structures, in order to form a kind of lines - specific queues - for organizing this data. For non-linear structures, we have graphs as a clear example, which use other means of organization (which we will go into later).
16 |
17 | ### Homogeneous x Heterogeneous
18 |
19 | Here we have a difference in the types of data that can be stored in each node (a concept to be developed later). When we have data of the same type being manipulated we are dealing with a homogeneous structure, while data of different types form a heterogeneous structure.
20 |
21 | ### Static x Dynamic
22 |
23 | The amount of memory to be used is a factor that determines whether a data structure is static or dynamic, in which static structures have a fixed memory capacity always, while dynamic ones can expand and be easily manipulated.
24 |
25 | For recommendation of books to be studied about data structures I recommend [this publication](https://medium.com/javarevisited/10-best-books-for-data-structure-and-algorithms-for-beginners-in-java-c-c-and-python-5e3d9b478eb1) that shows a little about each of the main books.
26 |
27 | ---
28 |
29 | Let's see a little about some types of data structures?
30 |
31 |
32 | Next -> Types of data structures
33 |
34 |
35 |
36 | Back to main menu
37 |
38 |
--------------------------------------------------------------------------------
/content/linear/README.md:
--------------------------------------------------------------------------------
1 | # Linear
2 |
--------------------------------------------------------------------------------
/content/linear/array/README.md:
--------------------------------------------------------------------------------
1 | # Array
2 |
3 | An array is a collection of items in the memory stored contiguously. Arrays are used a lot because of the random access, which allows you to access directly any element of the collection. This causes arrays to be faster at reading. All elements in the array should be of the same type.
4 |
--------------------------------------------------------------------------------
/content/linear/array/array/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | version = 3
4 |
5 | [[package]]
6 | name = "array"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/content/linear/array/array/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "array"
3 | version = "0.1.0"
4 | edition = "2021"
5 |
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 |
8 | [dependencies]
9 |
--------------------------------------------------------------------------------
/content/linear/array/array/src/main.rs:
--------------------------------------------------------------------------------
1 | fn main() {
2 | /*
3 | By default variables in Rust are not mutable, so this would be the implementation
4 | of an array of integers in Rust with the variable "numbers" being immutable!
5 | */
6 |
7 | let numbers: [i32; 10] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
8 |
9 | println!("The first number is: {}", numbers[0]);
10 |
11 | /*
12 | We have practically the same implementation as the one shown above, but this time
13 | defining the variable as mutable!
14 | */
15 |
16 | let mut new_numbers: [i32; 10] = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
17 |
18 | println!("The first number is: {}", new_numbers[0]);
19 |
20 | new_numbers[0] = 11;
21 |
22 | println!("Now, the first number is: {}", new_numbers[0]);
23 | }
24 |
--------------------------------------------------------------------------------
/content/linear/linked/README.md:
--------------------------------------------------------------------------------
1 | # Linked lists
2 |
3 | With linked lists, the items are not stored in the memory contiguously. Comparing to arrays, linked lists are good at insertion and deletion, but are slower than arrays at reading due to the sequencial access, in which each item stores the address of the next one, making it slower to access the last item of a linked list, for example. Arrays are faster at reading because they use random access.
4 |
5 | ---
6 |
7 | What did you think of finishing the content talking about linked lists? Let's learn now about tree data structures?
8 |
9 |
10 | Next -> Tree data structures
11 |
12 |
13 |
14 | Back to main menu
15 |
16 |
--------------------------------------------------------------------------------
/content/linear/queue/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lanjoni/rust-data-structure/c5bae9ae79f1fdba98ed735dd3d6947b02020982/content/linear/queue/README.md
--------------------------------------------------------------------------------
/content/linear/stack/README.md:
--------------------------------------------------------------------------------
1 | # Stack
2 |
3 | Stack is a memory space that is used for allocating primitive types of temporary information. The reservation of the amount of memory needed for such a function is generated from calculations made by the Rust compiler itself, this storage space already has its own dynamic size due to the dependence of its demand, however, it is up to the program to add or remove elements within it.
4 |
5 | ```
6 | blablabla
7 | ```
8 |
9 | ---
10 |
11 | What did you think of learning about stacks? Ready to learn a little about implementing queues in Rust?
12 |
13 |
14 | Next -> Queue
15 |
16 |
17 |
18 | Back to main menu
19 |
20 |
--------------------------------------------------------------------------------
/content/tree/README.md:
--------------------------------------------------------------------------------
1 | # Tree
2 |
--------------------------------------------------------------------------------
/content/tree/binary-tree/README.md:
--------------------------------------------------------------------------------
1 | # Binary tree
2 |
--------------------------------------------------------------------------------
/content/tree/binary-tree/basic-insert/Cargo.lock:
--------------------------------------------------------------------------------
1 | # This file is automatically @generated by Cargo.
2 | # It is not intended for manual editing.
3 | version = 3
4 |
5 | [[package]]
6 | name = "binary-tree"
7 | version = "0.1.0"
8 |
--------------------------------------------------------------------------------
/content/tree/binary-tree/basic-insert/Cargo.toml:
--------------------------------------------------------------------------------
1 | [package]
2 | name = "binary-tree"
3 | version = "0.1.0"
4 | edition = "2021"
5 |
6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7 |
8 | [dependencies]
9 |
--------------------------------------------------------------------------------
/content/tree/binary-tree/basic-insert/README.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lanjoni/rust-data-structure/c5bae9ae79f1fdba98ed735dd3d6947b02020982/content/tree/binary-tree/basic-insert/README.md
--------------------------------------------------------------------------------
/content/tree/binary-tree/basic-insert/src/classes/mod.rs:
--------------------------------------------------------------------------------
1 | pub mod tree;
--------------------------------------------------------------------------------
/content/tree/binary-tree/basic-insert/src/classes/tree.rs:
--------------------------------------------------------------------------------
1 | use std::fmt;
2 |
3 | pub struct Tree {
4 | pub value: i32,
5 | pub left: Option>,
6 | pub right: Option>
7 | }
8 |
9 | impl Tree {
10 | pub fn new(value: i32) -> Self {
11 | Tree {
12 | value,
13 | left: None,
14 | right: None
15 | }
16 | }
17 |
18 | pub fn insert(&mut self, value: i32) {
19 | if value < self.value {
20 | match &mut self.left {
21 | None => self.left = Some(Box::new(Tree::new(value))),
22 | Some(subtree) => subtree.insert(value),
23 | }
24 | } else {
25 | match &mut self.right {
26 | None => self.right = Some(Box::new(Tree::new(value))),
27 | Some(subtree) => subtree.insert(value),
28 | }
29 | }
30 | }
31 |
32 | pub fn preorder(&self) -> String {
33 | let mut result = String::new();
34 | result.push_str(&self.value.to_string());
35 | result.push_str("\n");
36 | if let Some(node) = &self.left {
37 | result.push_str(&node.preorder());
38 | }
39 | if let Some(node) = &self.right {
40 | result.push_str(&node.preorder());
41 | }
42 | result
43 | }
44 |
45 | pub fn inorder(&self) -> String {
46 | let mut result = String::new();
47 | if let Some(node) = &self.left {
48 | result.push_str(&node.inorder());
49 | }
50 | result.push_str(&self.value.to_string());
51 | result.push_str("\n");
52 | if let Some(node) = &self.right {
53 | result.push_str(&node.inorder());
54 | }
55 | result
56 | }
57 |
58 | pub fn posorder(&self) -> String {
59 | let mut result = String::new();
60 | if let Some(node) = &self.left {
61 | result.push_str(&node.posorder());
62 | }
63 | if let Some(node) = &self.right {
64 | result.push_str(&node.posorder());
65 | }
66 | result.push_str(&self.value.to_string());
67 | result.push_str("\n");
68 | result
69 | }
70 |
71 | }
72 |
73 | impl fmt::Display for Tree {
74 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 | write!(f, "{}", self.value)?;
76 | if self.left.is_some() {
77 | write!(f, " ({})", self.left.as_ref().unwrap())?;
78 | }
79 | if self.right.is_some() {
80 | write!(f, " ({})", self.right.as_ref().unwrap())?;
81 | }
82 | Ok(())
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/content/tree/binary-tree/basic-insert/src/main.rs:
--------------------------------------------------------------------------------
1 | mod classes;
2 |
3 | use classes::tree::Tree;
4 |
5 | fn main() {
6 | let mut tree = Tree::new(2);
7 |
8 | tree.insert(5);
9 | tree.insert(1);
10 | tree.insert(3);
11 |
12 | println!("\nPre order: \n{}", tree.preorder());
13 | println!("\nIn order: \n{}", tree.inorder());
14 | println!("\nPos order: \n{}", tree.posorder());
15 | }
--------------------------------------------------------------------------------
/resources/README.md:
--------------------------------------------------------------------------------
1 | # Resources
2 |
--------------------------------------------------------------------------------
/resources/images/README.md:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/resources/images/logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/lanjoni/rust-data-structure/c5bae9ae79f1fdba98ed735dd3d6947b02020982/resources/images/logo.png
--------------------------------------------------------------------------------