rust_pixel 2.4.0

2d pixel-art game engine & rapid prototype tools support terminal, wgpu and web...
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
---
title: The Rust Programming Language
author: Rust Tutorial
theme: dark
transition: cycle
title_animation: typewriter
code_theme: base16-ocean.dark
margin: 4
height: 28
transition_duration: 0.2
---

# The Rust Programming Language

A systems language focused on safety, speed, and concurrency

Press Space or Right Arrow to advance

---

## Why Rust?

<!-- anim: spotlight -->
Memory Safety - Performance - Zero-Cost Abstractions

<!-- pause -->

* **Memory safe** without garbage collection (ownership system)
* **Performance** on par with C/C++, no runtime overhead
* **Concurrency** without data races (compile-time guarantees)
* **Modern tooling:** Cargo, rustfmt, clippy, rust-analyzer

<!-- pause -->

> [!note]
> Rust has been voted "most admired language" for years.
> Adopted by Linux, Windows, Android, and Chromium kernels.

---

## Rust vs Other Languages

```barchart
title: Overall Score (Safety+Perf+Ecosystem)
labels: [Rust, C++, Go, Java, Python, Zig]
values: [95, 78, 82, 75, 68, 80]
height: 12
```

---

## Installation

<!-- column_layout: [1, 1] -->
<!-- column: 0 -->

### Install rustup

```bash
curl --proto '=https' \
  --tlsv1.2 -sSf \
  https://sh.rustup.rs | sh
```

<!-- column: 1 -->

### Verify

```bash
rustc --version
cargo --version
rustup --version
```

<!-- reset_layout -->

<!-- pause -->

> [!note]
> rustup manages multiple toolchains.
> Switch between stable, beta, and nightly easily.

---

## Hello, World!

```rust +line_numbers
fn main() {
    println!("Hello, world!");
}
```

<!-- pause -->

<!-- divider -->

Build and run:

```bash +line_numbers
# Compile directly
rustc main.rs && ./main

# Using Cargo (recommended)
cargo new hello_rust
cd hello_rust && cargo run
```

---

## Variables and Mutability

```rust {1-3|5-7|all} +line_numbers
// Immutable by default
let x = 5;
// x = 6;  // ERROR: cannot assign twice

// Mutable variables
let mut y = 10;
y = 20;  // OK
```

<!-- pause -->

<!-- divider -->

```rust +line_numbers
// Constants (compile-time, type annotation required)
const MAX_POINTS: u32 = 100_000;

// Shadowing (re-declare with let)
let x = 5;
let x = x + 1;    // x = 6
let x = x * 2;    // x = 12
```

---

## Primitive Data Types

<!-- column_layout: [1, 1] -->
<!-- column: 0 -->

### Scalar Types

| Type    | Examples          |
|:--------|:------------------|
| Integer | `i8` to `i128`    |
| Unsigned| `u8` to `u128`    |
| Float   | `f32, f64`        |
| Boolean | `true, false`     |
| Char    | `'A', 'Z'`       |

<!-- column: 1 -->

### Compound Types

| Type    | Examples          |
|:--------|:------------------|
| Tuple   | `(i32, f64, bool)`|
| Array   | `[i32; 5]`        |
| Slice   | `&[i32]`          |
| String  | `String, &str`    |

<!-- reset_layout -->

---

## Functions

```rust +line_numbers
// Parameters require type annotations
// Return type declared with ->
fn add(a: i32, b: i32) -> i32 {
    a + b  // last expression = return value
}

// if is an expression (returns a value)
fn classify(n: i32) -> &'static str {
    if n > 0 { "positive" }
    else if n < 0 { "negative" }
    else { "zero" }
}
```

<!-- pause -->

> [!note]
> Almost everything in Rust is an expression.
> if, match, and blocks {} can all return values.

---

## Control Flow

```rust {1-4|6-10|12-16|all} +line_numbers
// for loop (most common)
for i in 0..5 {
    println!("{}", i);  // 0,1,2,3,4
}

// while loop
let mut n = 3;
while n > 0 {
    n -= 1;
}

// loop: infinite, break returns a value
let result = loop {
    if n == 10 { break n * 2; }
    n += 1;
};
```

---

## Ownership

<!-- anim: fadein -->
The core concept that guarantees memory safety without GC

<!-- pause -->

**Three rules:**

1. Each value has exactly one owner
2. There can only be one owner at a time
3. When the owner goes out of scope, the value is dropped

<!-- pause -->

```rust +line_numbers
let s1 = String::from("hello");
let s2 = s1;  // ownership MOVED to s2
// println!("{}", s1); // ERROR: s1 is invalid
println!("{}", s2);    // OK
```

---

## Clone and Copy

```rust {1-3|5-8|all} +line_numbers
// Deep copy (Clone) - heap data
let s1 = String::from("hello");
let s2 = s1.clone();  // s1 still valid

// Stack data: automatic Copy
let x = 5;
let y = x;    // x still valid
println!("x={}, y={}", x, y);
```

<!-- pause -->

> [!note]
> Types implementing the Copy trait (integers, floats,
> booleans, chars, tuples of Copy types) are copied
> automatically on assignment. String, Vec do NOT Copy.

---

## Borrowing: Immutable References

```rust +line_numbers
fn calculate_length(s: &String) -> usize {
    s.len()
    // s goes out of scope, but since it doesn't
    // have ownership, nothing happens
}

let s = String::from("hello");
let len = calculate_length(&s);
// s is still valid here
println!("{}: {} chars", s, len);
```

* Multiple immutable references allowed simultaneously

---

## Borrowing: Mutable References

```rust +line_numbers
fn append_world(s: &mut String) {
    s.push_str(", world!");
}

let mut s = String::from("hello");
append_world(&mut s);
println!("{}", s);  // "hello, world!"
```

<!-- pause -->

<!-- divider -->

**Borrowing rules:**

* At any time: EITHER multiple `&T` OR one `&mut T`
* References must always be valid (no dangling)

<!-- pause -->

> [!caution]
> Violating borrowing rules causes compile errors,
> NOT runtime crashes. This is a feature!

---

## Structs

```rust +line_numbers
struct User {
    name: String,
    email: String,
    active: bool,
}

impl User {
    fn new(name: &str, email: &str) -> Self {
        User {
            name: name.to_string(),
            email: email.to_string(),
            active: true,
        }
    }

    fn greet(&self) -> String {
        format!("Hi, I'm {}", self.name)
    }
}
```

---

## Enums and Pattern Matching

```rust +line_numbers
enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
    Triangle(f64, f64),
}

fn area(s: &Shape) -> f64 {
    match s {
        Shape::Circle(r) =>
            std::f64::consts::PI * r * r,
        Shape::Rectangle(w, h) => w * h,
        Shape::Triangle(b, h) => 0.5 * b * h,
    }
}
```

<!-- pause -->

> [!note]
> match must be exhaustive: the compiler ensures
> every possible case is handled. Use _ for catch-all.

---

## Option and Result

```rust {1-5|7-11|all} +line_numbers
// Option<T>: values that might be absent
fn find_user(id: u32) -> Option<String> {
    if id == 1 { Some("Alice".into()) }
    else { None }
}

// Result<T, E>: operations that might fail
use std::fs;

fn read_config() -> Result<String, std::io::Error> {
    fs::read_to_string("config.toml")
}
```

<!-- pause -->

<!-- divider -->

```rust +line_numbers
// ? operator: concise error propagation
fn load() -> Result<String, std::io::Error> {
    let content = fs::read_to_string("a.txt")?;
    Ok(content.to_uppercase())
}
```

---

## Traits

<!-- anim: wave -->
Defining shared behavior - the foundation of polymorphism

<!-- pause -->

```rust +line_numbers
trait Summary {
    fn summarize(&self) -> String;

    // Default implementation
    fn preview(&self) -> String {
        format!("{}...", &self.summarize()[..20])
    }
}

struct Article { title: String, body: String }

impl Summary for Article {
    fn summarize(&self) -> String {
        format!("{}: {}", self.title, self.body)
    }
}
```

---

## Generics

```rust +line_numbers
// Generic function with trait bound
fn largest<T: PartialOrd>(list: &[T]) -> &T {
    let mut max = &list[0];
    for item in &list[1..] {
        if item > max { max = item; }
    }
    max
}
```

<!-- pause -->

<!-- divider -->

```rust +line_numbers
// Generic struct + trait bound
struct Point<T> { x: T, y: T }

impl<T: std::fmt::Display> Point<T> {
    fn show(&self) {
        println!("({}, {})", self.x, self.y);
    }
}
```

---

## Lifetimes

```rust +line_numbers
// Lifetime annotations tell the compiler
// how long references are valid
fn longest<'a>(
    x: &'a str,
    y: &'a str,
) -> &'a str {
    if x.len() > y.len() { x } else { y }
}
```

<!-- pause -->

<!-- divider -->

```rust +line_numbers
// Structs holding references need lifetimes
struct Excerpt<'a> {
    part: &'a str,
}

let novel = String::from("Once upon a time...");
let first = novel.split('.').next().unwrap();
let e = Excerpt { part: first };
```

---

## Closures and Iterators

```rust +line_numbers
// Closures: anonymous functions capturing env
let add = |a, b| a + b;
println!("{}", add(2, 3));  // 5

// Iterator chains: lazy, composable
let v = vec![1, 2, 3, 4, 5, 6];
let sum: i32 = v.iter()
    .filter(|&&x| x % 2 == 0)  // even
    .map(|&x| x * x)           // square
    .sum();                     // total
println!("{}", sum);  // 4+16+36 = 56
```

<!-- pause -->

> [!note]
> Iterators are zero-cost abstractions: compiled code
> performs identically to hand-written loops.

---

## Collections

<!-- column_layout: [1, 1] -->
<!-- column: 0 -->

### Vec and String

```rust +line_numbers
let mut v = vec![1, 2, 3];
v.push(4);
v.pop();

let mut s = String::new();
s.push_str("hello");
s += " world";
```

<!-- column: 1 -->

### HashMap

```rust +line_numbers
use std::collections::HashMap;

let mut m = HashMap::new();
m.insert("key", 42);

if let Some(v) = m.get("key") {
    println!("{}", v);
}

m.entry("new").or_insert(0);
```

<!-- reset_layout -->

---

## Concurrency

```rust +line_numbers
use std::thread;
use std::sync::{Arc, Mutex};

fn main() {
    let counter = Arc::new(Mutex::new(0));
    let mut handles = vec![];

    for _ in 0..10 {
        let c = Arc::clone(&counter);
        handles.push(thread::spawn(move || {
            *c.lock().unwrap() += 1;
        }));
    }

    for h in handles { h.join().unwrap(); }
    println!("{}", *counter.lock().unwrap());
}
```

---

## Smart Pointers

| Type          | Purpose                     |
|:--------------|:----------------------------|
| `Box<T>`      | Heap allocation, single owner |
| `Rc<T>`       | Reference counted, shared   |
| `Arc<T>`      | Atomic ref count, thread-safe |
| `RefCell<T>`  | Runtime borrow checking     |
| `Mutex<T>`    | Mutual exclusion lock       |

<!-- pause -->

```rust +line_numbers
use std::rc::Rc;

let a = Rc::new(vec![1, 2, 3]);
let b = Rc::clone(&a);  // ref count + 1
println!("count = {}", Rc::strong_count(&a));
// count = 2
```

---

## Cargo and Ecosystem

| Command         | Description         |
|:----------------|:--------------------|
| `cargo new`     | Create new project  |
| `cargo build`   | Build project       |
| `cargo run`     | Build and run       |
| `cargo test`    | Run tests           |
| `cargo doc`     | Generate docs       |
| `cargo clippy`  | Lint checks         |
| `cargo fmt`     | Format code         |

---

## Learning Roadmap

```mermaid
graph TD
A[Basic Syntax] --> B[Ownership]
B --> C[Structs and Enums]
C --> D[Traits and Generics]
D --> E{Advanced Path}
E -->|Systems| F[unsafe/FFI]
E -->|Web| G[Actix/Axum]
E -->|Async| H[tokio/async]
```

---

## Client-Server Interaction

<!-- spacer: 5 -->

```mermaid
sequenceDiagram
    participant C as Client
    participant S as Server
    participant D as Database
    C->>S: HTTP Request
    S->>D: Query
    D-->>S: Results
    S-->>C: HTTP Response
```

---

<!-- jump_to_middle -->

# Thank You!

Safe, Fast, Concurrent - Choose all three.