You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/docs/b1.vectors.md
+10-10Lines changed: 10 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,28 +1,28 @@
1
1
title: Vectors
2
2
---
3
3
4
-
If you remember, array is a fixed-size list of elements, of same data type. Even with mut, its element count can not be changed. A vector is kind of **a re-sizable array** but **all elements must be in the same type**.
4
+
If you remember, the array is a fixed-size list of elements, of the same data type. Even with mut, its element count cannot be changed. A vector is kind of **a re-sizable array** but **all elements must be in the same type**.
5
5
6
-
⭐️ It’s a generic type, written as **`Vec<T>`** . T can have any type, ex. The type of a Vec of i32s is `Vec<i32>`. Also, Vectors always allocate their data in dynamically allocated heap.
6
+
⭐️ It’s a generic type, written as **`Vec<T>`** . T can have any type, ex. The type of a Vec of i32s is `Vec<i32>`. Also, Vectors always allocate their data in a dynamically allocated heap.
7
7
8
8
### Create empty vector
9
9
10
10
```rust
11
-
letmuta=Vec::new(); //1.with new() keyword
12
-
letmutb=vec![]; //2.using the vec! macro
11
+
letmuta=Vec::new(); //1.With new() keyword
12
+
letmutb=vec![]; //2.Using the vec! macro
13
13
```
14
14
15
15
### Create with data types
16
16
17
17
```rust
18
18
letmuta2:Vec<i32> =Vec::new();
19
19
letmutb2:Vec<i32> =vec![];
20
-
letmutb3=vec![1i32, 2, 3];//sufixing 1st value with data type
20
+
letmutb3=vec![1i32, 2, 3];//Sufixing 1st value with data type
//c[6] = 2; can't assign values this way, index out of bounds
35
+
//c[6] = 2; Cannot assign values this way, index out of bounds
36
36
println!("{:?}", c); //[1, 2, 3, 2, 1]
37
37
38
38
//push and pop
@@ -55,13 +55,13 @@ e.push(11);
55
55
```
56
56
57
57
⭐️ Mainly a vector represent 3 things,
58
-
-a**pointer** to the data
58
+
-A**pointer** to the data
59
59
-**No of elements** currently have(**length**)
60
-
-**capacity** (Amount of space allocated for any future elements).
60
+
-**Capacity** (Amount of space allocated for any future elements).
61
61
62
62
If the length of a vector exceeds its capacity, its capacity will be increased automatically. But its elements will be reallocated(which can be slow). So always use Vec::**with_capacity** whenever it’s possible.
63
63
64
-
> 💡 **String** data type is a UTF-8 encoded vector. But you can not index into a String because of encoding.
64
+
> 💡 The **String** data type is a UTF-8 encoded vector. But you can not index into a String because of encoding.
65
65
66
66
67
67
💯 Vectors can be used with iterators in three ways,
Copy file name to clipboardExpand all lines: source/docs/b2.structs.md
+27-27Lines changed: 27 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
1
title: Structs
2
2
---
3
3
4
-
⭐️ Structs are used to **encapsulate related properties** into one unified datatype.
4
+
⭐️ Structs are used to **encapsulate related properties** into one unified data type.
5
5
6
6
💡 By convention, the name of the struct starts with a capital letter and follows **CamelCase**.
7
7
8
8
There are 3 variants of structs,
9
9
1.**C-like structs**
10
-
*one or more commaseparated name:value pairs
11
-
*brace-enclosed list
12
-
*similar to classes \(without its methods\) in OOP languages
13
-
*because fields have names, we can access them through dot notation
10
+
*One or more comma-separated name:value pairs
11
+
*Brace-enclosed list
12
+
*Similar to classes \(without its methods\) in OOP languages
13
+
*Because fields have names, we can access them through dot notation
14
14
15
15
2.**Tuple structs**
16
-
*one or more commaseparated values
17
-
* parenthesized list like tuples
18
-
*looks like a named tuples
16
+
*One or more comma-separated values
17
+
*A parenthesized list like tuples
18
+
*Looks like a named tuples
19
19
20
20
3.**Unit structs**
21
-
*a struct with no members at all
22
-
*it defines a new type but it resembles an empty tuple, \(\)
23
-
*rarely in use, useful with generics
21
+
*A struct with no members at all
22
+
*It defines a new type but it resembles an empty tuple, \(\)
23
+
*Rarely in use, useful with generics
24
24
25
25
⭐️ When regarding OOP in Rust, attributes and methods are placed separately on **structs** and **traits**. Structs contain only attributes, traits contain only methods. They are getting connected via **impls**.
println!("The distance: {} km", distance_in_km); //The distance: 20 km
94
94
}
95
95
```
96
96
97
97
## Unit structs
98
98
99
-
This is rarely useful on its own, but in combination with other features it can become useful.
99
+
This is rarely useful on its own. But in combination with other features, it can become useful.
100
100
101
101
> [📖](https://doc.rust-lang.org/book/first-edition/structs.html) ex: A library may ask you to create a structure that implements a certain trait to handle events. If you don’t have any data you need to store in the structure, you can create a unit-like struct.
Copy file name to clipboardExpand all lines: source/docs/b4.generics.md
+20-20Lines changed: 20 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,18 +3,18 @@ title: Generics
3
3
4
4
> [📖](https://doc.rust-lang.org/beta/book/first-edition/generics.html) Sometimes, when writing a function or data type, we may want it to work for multiple types of arguments. In Rust, we can do this with generics.
5
5
6
-
💭 The concept is, instead of declaring a specific data type we use an uppercase letter(or CamelCase identifier). ex, **instead x : u8** we use **x : T** . but we have to inform to the compiler that T is a generic type(can be any type) by adding `<T>` at first.
6
+
💭 The concept is, instead of declaring a specific data type we use an uppercase letter(or CamelCase identifier). ex, **instead of x : u8** we use **x : T** . but we have to inform to the compiler that T is a generic type(can be any type) by adding `<T>` at first.
7
7
8
8
### Generalizing functions
9
9
10
10
```rust
11
11
fntakes_anything<T>(x:T) { // x has type T, T is a generic type
12
12
}
13
13
14
-
fntakes_two_of_the_same_things<T>(x:T, y:T) { //both x and y has same type
14
+
fntakes_two_of_the_same_things<T>(x:T, y:T) { //Both x and y has the same type
0 commit comments