1
+
2
+ ---
3
+ layout: section
4
+ ---
5
+
6
+ # Basic Syntax
7
+
1
8
---
2
9
3
10
# Variables
@@ -84,6 +91,7 @@ that variable
84
91
``` rust
85
92
fn main () {
86
93
let x : i32 = 20 ;
94
+ // ^^^^^ Type annotation
87
95
}
88
96
```
89
97
@@ -98,7 +106,7 @@ layout: two-cols
98
106
# Integers
99
107
100
108
| Length | Signed | Unsigned |
101
- | --------------- | --------- | ---------- |
109
+ | ------------- | ------- | -------- |
102
110
| 8 bits | ` i8 ` | ` u8 ` |
103
111
| 16 bits | ` i16 ` | ` u16 ` |
104
112
| 32 bits | ` i32 ` | ` u32 ` |
@@ -242,14 +250,14 @@ then the code for b is not executed
242
250
243
251
``` rust
244
252
fn main () {
245
- let c = 'z' ;
253
+ let c : char = 'z' ;
246
254
let z = 'ℤ' ;
247
255
let heart_eyed_cat = '😻' ;
248
256
}
249
257
```
250
258
251
- - A character is a 32-bit unicode scalar value
252
- - Very much unlike C/C++ where char is 8 bits
259
+ - A ` char ` is a 32-bit unicode scalar value
260
+ - Very much unlike C/C++ where ` char is 8 bits
253
261
254
262
<!--
255
263
- The final scalar type is the character, but it isn't often seen.
@@ -261,16 +269,22 @@ instead.
261
269
262
270
---
263
271
264
- # Strings
272
+ # ` String ` s
265
273
``` rust
266
- // Owned, heap-allocated string *slice*
267
- let s1 : String = String :: new (" Hello, 🌍!" );
274
+
275
+ let s1 = String :: new (" Hello, 🌍!" );
276
+ // ^^^^^^ Owned, heap-allocated string
268
277
```
269
278
270
- - Rust strings are UTF-8-encoded
279
+ - Rust ` String ` s are UTF-8-encoded
271
280
- Unlike C/C++: * Not null-terminated*
272
281
- Cannot be indexed like C strings
282
+ - ` String ` is heap-allocated
273
283
- Actually many types of strings in Rust
284
+ - ` CString `
285
+ - ` PathBuf `
286
+ - ` OsString `
287
+ - ...
274
288
275
289
<!--
276
290
- Rusts strings are complicated, because all strings are complicated
@@ -294,7 +308,7 @@ fn main() {
294
308
- Group multiple values into a single compound type
295
309
- Fixed size
296
310
- Different types per element
297
- - Create a tuple by writing a comma-separated list of values inside parentheses
311
+ - Create by writing a comma-separated list of values inside parentheses
298
312
299
313
::right::
300
314
@@ -406,9 +420,9 @@ fn also_returns_nothing() {
406
420
```
407
421
408
422
- The function boundary must always be explicitly annotated with types
409
- - Within the function body type inference may be used
423
+ - Type inference may be used in function body
410
424
- A function that returns nothing has the return type * unit* (` () ` )
411
- - The function body contains a series of statements optionally ending with an
425
+ - Function body contains a series of statements optionally ending with an
412
426
expression
413
427
414
428
<!--
@@ -440,6 +454,10 @@ fn my_fun() {
440
454
let x = 10 ;
441
455
```
442
456
457
+ ``` rust
458
+ return 42 ;
459
+ ```
460
+
443
461
<v-click >
444
462
445
463
``` rust
@@ -465,9 +483,9 @@ statements
465
483
466
484
- Expressions evaluate to a resulting value
467
485
- Expressions make up most of the Rust code you write
468
- - Includes all control flow such as ` if ` and ` while `
486
+ - Includes all control flow such as ` if ` and ` loop `
469
487
- Includes scoping braces (` { ` and ` } ` )
470
- - An expression can be turned into a statement by adding a semicolon ( ` ; ` )
488
+ - Semicolon ( ` ; ` ) turns expression into statement
471
489
472
490
``` rust {all|2-5}
473
491
fn main () {
0 commit comments