Skip to content

Commit a91e777

Browse files
committed
🦀 Update to current Edition
1 parent 599ec81 commit a91e777

14 files changed

+739
-423
lines changed

content/en/docs/a1.why-rust.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ Since Rust 1.0, major updates have been released as [`Editions`](/docs/cargo-cra
1111

1212
## Initial Goals
1313

14-
The goal of Rust is to be a good programming language for creating highly concurrent, safe and performant systems.
14+
> Rust is a systems programming language focused on three goals: safety, speed, and concurrency.
15+
> </br>~ Rust Documentation
1516
16-
> **"Rust is a systems programming language focused on three goals: safety, speed, and concurrency."
17-
> </br>~ Rust Documentation**
17+
**Rust is a modern, [multi-platform](https://doc.rust-lang.org/rustc/platform-support.html), [multi-paradigm](https://en.wikipedia.org/wiki/Comparison_of_multi-paradigm_programming_languages), [statically compiled](https://en.wikipedia.org/wiki/Compiled_language), memory & thread safety–focused, systems programming language.**
1818

19-
Rust is a very young and very modern language. It's a **[compiled programming language](https://en.wikipedia.org/wiki/Compiled_language)** and it uses [LLVM](https://en.wikipedia.org/wiki/LLVM) on the backend. Also, Rust is a **[multi-paradigm programming language](https://en.wikipedia.org/wiki/Comparison_of_multi-paradigm_programming_languages)**, which supports imperative procedural, concurrent actor, object-oriented and pure functional styles. It also supports generic programming and metaprogramming, in both static and dynamic styles.
19+
- It uses [LLVM](https://en.wikipedia.org/wiki/LLVM) on the backend and supports many different operating systems, architectures, and targets.
20+
- It supports imperative procedural, concurrent actor, object-oriented, and pure functional styles. Rust also supports generic programming and metaprogramming, in both static and dynamic styles.
21+
- It doesn't use a built-in runtime or an automated garbage collection system \(GC\).
2022

21-
> One of Rust’s most unique and compelling features is [Ownership](/docs/ownership), which is used to achieve memory safety. Rust creates memory pointers optimistically, checks memory pointers’ limited accesses at compile-time with the usage of [References and Borrowing](/docs/borrowing). And it does automatic compile-time memory management by checking the [Lifetimes](/docs/lifetimes).
23+
> 💡 However, async Rust requires an async runtime, which is provided by community-maintained crates like [`tokio`](https://github.com/tokio-rs/tokio), [`soml`](https://github.com/smol-rs/smol) etc. The async runtime will be bundled into the final executable.
24+
25+
- One of Rust’s most unique and compelling features is [Ownership](/docs/ownership), which is used to achieve memory safety. Rust creates memory pointers optimistically, checks memory pointers’ limited accesses at compile-time with the usage of [References and Borrowing](/docs/borrowing). And it does automatic compile-time memory management by checking the [Lifetimes](/docs/lifetimes).
2226

2327
## Influences
2428

@@ -38,11 +42,7 @@ Its design elements came from a wide range of sources.
3842

3943
and etc.
4044

41-
Rust **doesn't use a built-in runtime** or an automated garbage collection system \(GC\).
42-
43-
> 💡 However, async Rust requires an async runtime, which is provided by community-maintained crates like [`tokio`](https://github.com/tokio-rs/tokio), [`async-std`](https://github.com/async-rs/async-std), [`soml`](https://github.com/smol-rs/smol) etc. The async runtime will be bundled into the final executable.
44-
45-
Rust compiler **observes the code at compile-time** and helps to [prevent many types of errors](https://doc.rust-lang.org/error-index.html) that are possible to write in C, C++ like programming languages.
45+
Rust compiler observes the code at compile-time and helps to [prevent many types of errors](https://doc.rust-lang.org/error-index.html) that are possible to write in C, C++ like programming languages.
4646

4747
## 👨‍🏫 Before going to the next...
4848

content/en/docs/a10.control-flows.md

Lines changed: 144 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,29 @@ println!("Current team size : {}", team_size); // Current team size : Medium
8181

8282
⭐️ **Return data type should be the same on each block when using this as an expression.**
8383

84-
8584
## match
8685

86+
- With multiple patterns
87+
8788
```rust
8889
let tshirt_width = 20;
8990
let tshirt_size = match tshirt_width {
90-
16 => "S", // check 16
91-
17 | 18 => "M", // check 17 and 18
92-
19 ..= 21 => "L", // check from 19 to 21 (19,20,21)
93-
22 => "XL",
94-
_ => "Not Available",
91+
13 => "XS", // check 13
92+
14 | 15 => "S", // check 14 and 15
93+
16..18 => "M", // check from 16 to 17 / 18 exclusive (16,17)
94+
18..=20 => "L", // check from 18 to 20 (18,19,20)
95+
96+
x if x > 20 && x < 26 => "XL", // check 21 to 25 (21,22,23,24,25)
97+
// >,>=,<,<= via assigning the value to a variable (x) + if
98+
99+
_ => "Not Available", // default behavior, if none of conditions matches
95100
};
96101

97102
println!("{}", tshirt_size); // L
98103
```
99104

105+
- Without default behavior
106+
100107
```rust
101108
let is_allowed = false;
102109
let list_type = match is_allowed {
@@ -109,6 +116,8 @@ let list_type = match is_allowed {
109116
println!("{}", list_type); // Restricted
110117
```
111118

119+
- With multiple variable matchings
120+
112121
```rust
113122
let marks_paper_a: u8 = 25;
114123
let marks_paper_b: u8 = 30;
@@ -124,162 +133,217 @@ let output = match (marks_paper_a, marks_paper_b) {
124133
println!("{}", output); // Work hard
125134
```
126135

127-
128136
## loop
129137

138+
- Infinite `loop`
139+
130140
```rust
141+
// ⚠️ This will run forever without terminating. Termininate manually (Ctrl+C)
131142
loop {
132-
println!("Loop forever!");
143+
println!("Loop forever!");
133144
}
134145
```
135146

147+
- With `break` and `continue`
148+
136149
```rust
137-
// Usage of break and continue
138150
let mut a = 0;
139151

140152
loop {
141-
if a == 0 {
142-
println!("Skip Value : {}", a);
143-
a += 1;
144-
continue;
145-
} else if a == 2 {
146-
println!("Break At : {}", a);
147-
break;
148-
}
149-
150-
println!("Current Value : {}", a);
151-
a += 1;
153+
if a == 0 {
154+
println!("Skip Value : {}", a);
155+
a += 1;
156+
continue;
157+
} else if a == 2 {
158+
println!("Break At : {}", a);
159+
break;
160+
}
161+
162+
println!("Current Value : {}", a);
163+
a += 1;
152164
}
153165
```
154166

167+
- Returning a value with `break`
168+
169+
```rust
170+
let (mut x, y) = (1, 10);
171+
172+
let z = loop {
173+
x *= 2; // x = 2,4,8,16...
174+
175+
if x >= y { // 16 >= 10, so return 16
176+
break x;
177+
}
178+
};
179+
180+
println!("{z}"); // 16
181+
```
182+
183+
- Labeling and `break` the outer `loop`
184+
155185
```rust
156-
// Outer break
157186
let mut b1 = 1;
158187

159-
'outer_loop: loop { //set label outer_loop
160-
let mut b2 = 1;
188+
'outer: loop { // set label outer (or any other snake_case name)
189+
let mut b2 = 1;
161190

162-
'inner_loop: loop {
163-
println!("Current Value : [{}][{}]", b1, b2);
191+
'inner: loop { // set label inner
192+
println!("Current Value : [{}][{}]", b1, b2);
164193

165-
if b1 == 2 && b2 == 2 {
166-
break 'outer_loop; // kill outer_loop
167-
} else if b2 == 5 {
168-
break;
169-
}
194+
if b1 == 2 && b2 == 2 {
195+
break 'outer; // kill outer loop
196+
} else if b2 == 5 {
197+
break;
198+
}
170199

171-
b2 += 1;
172-
}
200+
b2 += 1;
201+
}
173202

174-
b1 += 1;
203+
b1 += 1;
175204
}
176205
```
177206

178-
179207
## while
180208

209+
- Infinite `while`
210+
211+
```rust
212+
// ⚠️ This will run forever without terminating. Termininate manually (Ctrl+C)
213+
while true {
214+
println!("While forever!");
215+
}
216+
```
217+
218+
- A simple `while`
219+
181220
```rust
182221
let mut a = 1;
183222

184223
while a <= 10 {
185-
println!("Current value : {}", a);
186-
a += 1; //no ++ or -- on Rust
224+
println!("Current value : {}", a);
225+
a += 1; //no ++ or -- on Rust
187226
}
188227
```
189228

229+
- With `break` and `continue`
230+
190231
```rust
191-
// Usage of break and continue
192232
let mut b = 0;
193233

194234
while b < 5 {
195-
if b == 0 {
196-
println!("Skip value : {}", b);
197-
b += 1;
198-
continue;
199-
} else if b == 2 {
200-
println!("Break At : {}", b);
201-
break;
202-
}
203-
204-
println!("Current value : {}", b);
205-
b += 1;
235+
if b == 0 {
236+
println!("Skip value : {}", b);
237+
b += 1;
238+
continue;
239+
} else if b == 2 {
240+
println!("Break At : {}", b);
241+
break;
242+
}
243+
244+
println!("Current value : {}", b);
245+
b += 1;
206246
}
247+
248+
// 💡 You can't break with a value in a while
207249
```
208250

251+
- Labeling and `break` the outer `while`
252+
209253
```rust
210-
// Outer break
211254
let mut c1 = 1;
212255

213-
'outer_while: while c1 < 6 { //set label outer_while
214-
let mut c2 = 1;
256+
'outer: while c1 < 6 { // set label outer (or any other snake_case name)
257+
let mut c2 = 1;
258+
259+
'inner: while c2 < 6 { // set label inner
260+
println!("Current Value : [{}][{}]", c1, c2);
215261

216-
'inner_while: while c2 < 6 {
217-
println!("Current Value : [{}][{}]", c1, c2);
218-
if c1 == 2 && c2 == 2 { break 'outer_while; } //kill outer_while
219-
c2 += 1;
220-
}
262+
if c1 == 2 && c2 == 2 {
263+
break 'outer; // kill outer while
264+
}
221265

222-
c1 += 1;
266+
c2 += 1;
267+
}
268+
269+
c1 += 1;
223270
}
224271
```
225272

226-
227273
## for
228274

275+
- A simple `for`
276+
229277
```rust
230278
// 0 to 10 (10 exclusive); In other languages, `for(i = 0; i < 10; i++)`
231279
for i in 0..10 {
232-
println!("Current value : {}", i);
280+
println!("Current value : {}", i);
233281
}
234282
```
235283

236284
```rust
237285
// 1 to 10 (10 inclusive); In other languages, `for(i = 1; i <= 10; i++)`
238286
for i in 1..=10 {
239-
println!("Current value : {}", i);
287+
println!("Current value : {}", i);
240288
}
241289
```
242290

291+
- With `break` and `continue`
292+
243293
```rust
244-
// Usage of break and continue
294+
// 💡 You can't break with a value in a for
245295
for b in 0..6 {
246-
if b == 0 {
247-
println!("Skip Value : {}", b);
248-
continue;
249-
} else if b == 2 {
250-
println!("Break At : {}", b);
251-
break;
252-
}
253-
254-
println!("Current value : {}", b);
296+
if b == 0 {
297+
println!("Skip Value : {}", b);
298+
continue;
299+
} else if b == 2 {
300+
println!("Break At : {}", b);
301+
break;
302+
}
303+
304+
println!("Current value : {}", b);
255305
}
256306
```
257307

308+
- Labeling and `break` the outer `for`
309+
258310
```rust
259-
// Outer break
260-
'outer_for: for c1 in 1..6 { //set label outer_for
311+
'outer: for c1 in 1..6 { // set label outer (or any other snake_case name)
261312

262-
'inner_for: for c2 in 1..6 {
263-
println!("Current Value : [{}][{}]", c1, c2);
264-
if c1 == 2 && c2 == 2 { break 'outer_for; } //kill outer_for
265-
}
313+
'inner: for c2 in 1..6 { // set label inner
314+
println!("Current Value : [{}][{}]", c1, c2);
266315

316+
if c1 == 2 && c2 == 2 {
317+
break 'outer; // kill outer for
318+
}
319+
}
267320
}
268321
```
269322

323+
- Working with arrays and vectors
324+
270325
```rust
271-
// Working with arrays/vectors
272326
let group : [&str; 4] = ["Mark", "Larry", "Bill", "Steve"];
273327

274328
for n in 0..group.len() { // group.len() = 4 -> 0..4 👎 check group.len() on each iteration
275-
println!("Current Person : {}", group[n]);
329+
println!("Current Person : {}", group[n]);
276330
}
277331

278332
for person in group.iter() { // 👍 group.iter() turn the array into a simple iterator
279-
println!("Current Person : {}", person);
333+
println!("Current Person : {person}");
280334
}
281335

282336
for (index, person) in group.iter().enumerate() { // 💡 group.iter().enumerate() helps to read both the current index (starting from zero) and the value
283-
println!("Person {} : {}", index, person);
337+
println!("Person {index} : {person}");
338+
}
339+
```
340+
341+
- Working with vector of tuples
342+
343+
```rust
344+
let list = vec![(1, "Mark"), (2, "Larry"), (3, "Steve")];
345+
346+
for (index, person) in list {
347+
println!("Person {index} : {person}");
284348
}
285349
```

0 commit comments

Comments
 (0)