-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
1101 lines (1034 loc) · 45.4 KB
/
index.html
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
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Introduction to Rust</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@3.8.0/css/reset.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@3.8.0/css/reveal.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reveal.js@3.8.0/css/theme/black.min.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@9.16.2/build/styles/monokai.min.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'https://cdn.jsdelivr.net/npm/reveal.js@3.8.0/css/print/pdf.min.css' : 'https://cdn.jsdelivr.net/npm/reveal.js@3.8.0/css/print/paper.min.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<style>
.reveal .slides img.logo-img {
background: white;
border: 10px solid #fff;
border-radius: 10px;
}
.reveal .slides .font175 {
font-size: 175%;
}
.reveal .slides .font150 {
font-size: 150%;
}
.reveal .slides .font120 {
font-size: 120%;
}
.reveal .slides .font75 {
font-size: 75%;
}
.reveal .slides .font70 {
font-size: 70%;
}
.reveal .slides .font45 {
font-size: 45%;
}
.reveal .slides .underline {
text-decoration: underline;
}
.reveal .slides .mt0 {
margin-top: 0;
}
.reveal .slides .mb0 {
margin-bottom: 0;
}
.reveal .slides .white-bg {
background: white;
}
.reveal .slides pre > code > strong:not(.red) {
background-color: yellow;
color: black;
}
.reveal .slides pre > code > strong.red {
background-color: red;
color: black;
}
.reveal h1 {
font-size: 1.6em;
}
.reveal .title-slide h1 {
.font-size: 2.5em;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section class="center">
<h1>Introduction to Rust</h1>
</section>
<section id="what-is-rust" class="title-slide slide level1"><h1>What is <a href="https://www.rust-lang.org/">Rust</a>?</h1><p>A new language bringing features of modern programming languages to systems</p>
<p>…with a focus on safety, speed, and concurrency, and <em>zero-cost abstractions</em> (from C++’s <a href="https://isocpp.org/wiki/faq/big-picture#zero-overhead-principle">zero-overhead principle</a>).</p></section>
<section><section id="resources" class="title-slide slide level1"><h1><a href="https://www.rust-lang.org/learn">Resources</a></h1><p><a href="https://doc.rust-lang.org/book/">The Book</a>; <a href="https://doc.rust-lang.org/stable/rust-by-example/">Rust by Example</a></p>
<p><a href="https://doc.rust-lang.org/std/index.html">Standard library documentation</a></p>
<p><a href="https://doc.rust-lang.org/nomicon/index.html">Rustonomicon</a>; <a href="https://doc.rust-lang.org/reference/index.html">Reference</a></p></section><section id="rsdoc-search-engine" class="slide level2">
<h2>rsdoc search engine</h2>
<p>On Firefox, you can add <a href="https://doc.rust-lang.org/std/index.html?search=%s">this</a> as a keyword bookmark.</p>
</section></section>
<section><section id="installing-rust" class="title-slide slide level1"><h1>Installing Rust</h1><p>Use <a href="https://www.rust-lang.org/tools/install">Rustup</a>.</p></section><section id="why-rustup" class="slide level2">
<h2>Why Rustup?</h2>
<p>It’s common to want to use different versions of the compiler.</p>
<p>Rustup also helps you install new targets (to cross-compile).</p>
</section><section id="different-versions" class="slide level2">
<h2>Different versions?</h2>
<p>New features start off in Unstable Rust and “cook” for a while.</p>
<p>It is only possible to use unstable features using the Nightly build.</p>
<p>Also, you may need to test your project on older versions at times.</p>
</section><section id="cross-compilation" class="slide level2">
<h2>Cross-compilation</h2>
<p>Rust is based on LLVM, and so cross-compilation is supported out-of-the-box.</p>
<p>But to target a target, you need to have the standard library for that target. Rustup helps you install those additional things.</p>
</section></section>
<section id="hello-world" class="title-slide slide level1"><h1>Hello world</h1><pre class="rs"><code>fn main() {
println!("Hello world!");
}</code></pre>
<p>Compile and run</p>
<pre class="shell"><code>$ rustc helloworld.rs
$ ./helloworld
Hello world!</code></pre></section>
<section><section id="hello-cargo" class="title-slide slide level1"><h1>Hello Cargo</h1><p>Cargo is Rust’s build system and package manager.</p>
<pre class="shell"><code>$ cargo new --bin hello_cargo
$ cd hello_cargo</code></pre>
<h2>
<code>Cargo.toml</code>
</h2>
<p><code>Cargo.toml</code> is the package metadata file (akin to e.g. <code>package.json</code>, etc).</p>
<p>The format is <a href="https://github.com/toml-lang/toml">TOML</a>.</p></section><section id="cargo.toml" class="slide level2">
<h2><code>Cargo.toml</code></h2>
<pre class="toml"><code>[package]
name = "hello_cargo"
version = "0.1.0"
authors = ["Your Name <you@example.com>"]
edition = "2018"
[dependencies]</code></pre>
</section><section id="compiling-with-cargo" class="slide level2">
<h2>Compiling with Cargo</h2>
<p>Edit your program: <code>src/main.rs</code></p>
<pre class="rs"><code>fn main() {
println!("Hello Cargo!");
}</code></pre>
<p>Compile and run</p>
<pre><code>$ cargo build
$ target/debug/hello_cargo
# Or...
$ cargo run</code></pre>
</section><section id="moving-forward" class="slide level2">
<h2>Moving forward</h2>
<p>Until we start using Cargo features, you can either call <code>rustc</code> directly, or use Cargo.</p>
</section></section>
<section id="basic-syntax" class="title-slide slide level1"><h1>Basic syntax</h1><pre class="rs"><code>fn main() {
let x = true;
let mut y: u32 = 1u32;
if x {
println!("{}", y);
y += 1;
}
while y < 10 {
y += 1;
println!("{}", y);
}
}</code></pre></section>
<section id="basic-syntax-1" class="title-slide slide level1"><h1>Basic syntax</h1><pre class="rs"><code>fn add(i: i32, j: i32) -> i32 {
i + j // comment
}
/* comment */
/* /* nested comment */ */
fn main() { println!("{}", add(3, 5)); }</code></pre>
<p>The last expression in a block is returned.</p></section>
<section id="println" class="title-slide slide level1"><h1><code>println</code></h1><p><code>println</code> is a macro.<br />
Macros are invoked using <code>macro!(...)</code> (note the <code>!</code>).<br />
(We’ll go into macros in more detail later.)</p>
<p><a href="https://doc.rust-lang.org/std/fmt/index.html#usage">Format string syntax</a></p>
<p>Of note are <code>{}</code>, which is the generic display,<br />
and <code>{:?}</code>, which is the debug print specifier.<br />
(We’ll demonstrate the power of <code>{:?}</code> later.)</p></section>
<section id="primitive-scalar-types-br" class="title-slide slide level1"><h1>Primitive scalar types <a href="https://doc.rust-lang.org/book/ch03-02-data-types.html">[B]</a><a href="https://doc.rust-lang.org/stable/reference/expressions/literal-expr.html">[R]</a></h1><p>Boolean: <code>bool</code>: <code>true</code> or <code>false</code></p>
<p>Integers: {<code>i</code>, <code>u</code>}{<code>8</code>, <code>16</code>, <code>32</code>, <code>64</code>, <code>128</code>, <code>size</code>}<br />
e.g. <code>1</code>, <code>1u64</code>; default <code>i32</code></p>
<p>Floats: <code>f32</code>, <code>f64</code> e.g <code>2f64</code>, <code>2.</code>, <code>2.0</code><br />
default <code>f64</code></p>
<p>Unicode character: <code>char</code> e.g. <code>'a'</code>, <code>'🤔'</code></p>
<p>Unit type: <code>()</code></p></section>
<section id="number-literals-r" class="title-slide slide level1"><h1>Number literals <a href="https://doc.rust-lang.org/stable/reference/tokens.html#integer-literals">[R]</a></h1><p>Binary literals: <code>0b010101</code></p>
<p>Octal literals: <code>0o25</code></p>
<p>Hex literals: <code>0x15</code></p>
<p>Number separators: <code>123_456_789</code></p></section>
<section id="string-literals-r" class="title-slide slide level1"><h1>String literals <a href="https://doc.rust-lang.org/stable/reference/tokens.html#string-literals">[R]</a></h1><p>Strings: <code>"abc"</code></p>
<p>Raw strings:</p>
<pre class="rs"><code>r#"hello! I am a raw string
I can contain any number of " and # and r#" and //
and \ but I end the moment we have "# // this is a comment</code></pre>
<p>Also <a href="https://doc.rust-lang.org/stable/reference/tokens.html#byte-and-byte-string-literals">byte literals and byte string literals</a>.</p></section>
<section id="tuples-and-arrays-b" class="title-slide slide level1"><h1>Tuples and arrays <a href="https://doc.rust-lang.org/book/ch03-02-data-types.html#the-tuple-type">[B]</a></h1><pre class="rs"><code>let x: (i32, i32, i32) = (1, 2, 3);
let y: [i32; 3] = [1, 2, 3];
let z = [0u32; 500]; // array of 500 0u32</code></pre>
<p>Notice that the length of an array is part of its type.</p></section>
<section id="type-casting-r" class="title-slide slide level1"><h1>Type casting <a href="https://doc.rust-lang.org/stable/reference/expressions/operator-expr.html#type-cast-expressions">[R]</a></h1><p>Most commonly used to convert between numeric types:</p>
<pre class="rs"><code>let x: i32 = 5;
x as i16</code></pre></section>
<section><section id="expressions-and-statements-r" class="title-slide slide level1"><h1>Expressions and statements <a href="https://doc.rust-lang.org/stable/reference/expressions.html">[R]</a></h1><p>Rust is an <em>expression-based</em> language.</p>
<p>Most things are expressions, including <code>if</code>, and loops.</p>
<p>The only thing that is a statement is the <code>let</code> statement.</p></section><section id="block-expression" class="slide level2">
<h2>Block expression</h2>
<p>All blocks are expressions.</p>
<pre class="rs"><code>println!("{:?}", {
let mut y = 0;
while y < 10 { y += 1; }
y
});</code></pre>
<p>Blocks evaluate to the last expression evaluated.</p>
</section><section id="the-semicolon" class="slide level2">
<h2>The semicolon</h2>
<p><code>;</code> converts an expression to a statement (which is of type <code>()</code>), discarding the value.</p>
<pre class="rs"><code>println!("{:?}", {
let mut y = 0;
while y < 10 { y += 1; }
y;
});</code></pre>
</section><section id="if-expression-b" class="slide level2">
<h2><code>if</code> expression <a href="https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions">[B]</a></h2>
<p><code>if</code> is an expression.</p>
<pre class="rs"><code>let x = true;
println!("{}", if x { 1 } else { 0 });</code></pre>
<p>Note that both branches must evaluate to the same type (which could be <code>()</code>).</p>
</section><section id="while-loop-b" class="slide level2">
<h2><code>while</code> loop <a href="https://doc.rust-lang.org/book/ch03-05-control-flow.html#conditional-loops-with-while">[B]</a></h2>
<p>The <code>while</code> loop is an expression evaluating to <code>()</code>.</p>
<pre class="rs"><code>let mut x = 0;
println!("{:?}", while x < 10 { x += 1; });</code></pre>
</section><section id="for-loop-b" class="slide level2">
<h2><code>for</code> loop <a href="https://doc.rust-lang.org/book/ch03-05-control-flow.html#looping-through-a-collection-with-for">[B]</a></h2>
<p>The <code>for</code> loop is an expression evaluating to <code>()</code>.</p>
<pre class="rs"><code>let x = [1, 2, 3];
for y in &x {
println!("{}", y);
}</code></pre>
</section><section id="infinite-loop-b" class="slide level2">
<h2>Infinite loop <a href="https://doc.rust-lang.org/book/ch03-05-control-flow.html#repeating-code-with-loop">[B]</a></h2>
<pre class="rs"><code>let mut x = 0;
println!("{}", loop {
x += 1;
if x > 10 { break x; }
});</code></pre>
<p><code>loop</code> expressions can evaluate to a non-<code>()</code> value!</p>
</section></section>
<section id="range-expressions-r" class="title-slide slide level1"><h1>Range expressions <a href="https://doc.rust-lang.org/stable/reference/expressions/range-expr.html">[R]</a></h1><pre class="rs"><code>let x = [1, 2, 3, 4, 5];
for i in 0..x.len() {
println!("{}", x[i]);
}
for i in 0..=4 {
println!("{}", x[i]);
}</code></pre></section>
<section><section id="macros" class="title-slide slide level1"><h1>Macros</h1><p>Rust macros are <em>not</em> like CPP macros.</p>
<p>They operate on the AST level at compile-time.</p>
<p><a href="https://doc.rust-lang.org/std/index.html#macros">Standard library macros</a></p>
<p>Macro invocations generally look like <code>macro!(...)</code>.</p></section><section id="formatting-macros" class="slide level2">
<h2>Formatting macros</h2>
<p>Macros operate on the AST level at compile-time.</p>
<pre class="rs"><code>println!("Hello! {0} in hex is {0:02X}", 15);</code></pre>
<p>This means that format strings are actually parsed and type-checked <em>at compile-time</em>,</p>
<pre class="rs"><code>println!("Hello! {0} in hex is {0:02X}", "Hello");
// error[E0277]: the trait bound `str: std::fmt::UpperHex`
// is not satisfied</code></pre>
<p>…and are expanded out into function calls to produce the string you need.</p>
</section></section>
<section><section id="exercise-1" class="title-slide slide level1"><h1>Exercise 1</h1><p>To get you familiar with the syntax:</p>
<p>Implement FizzBuzz. In C: (i.e. translate this to Rust)</p>
<pre class="c"><code>int main() {
for (int i = 1; i <= 100; ++i) {
int m3 = i % 3 == 0, m5 = i % 5 == 0;
if (m3 || m5) {
printf("%s%s\n", m3 ? "Fizz" : "", m5 ? "Buzz" : "");
} else {
printf("%d\n", i);
}
}
}</code></pre></section><section id="exercise-1-1" class="slide level2">
<h2>Exercise 1</h2>
<pre class="rs"><code>fn main() {
for i in 1..=100 {
let m3 = i % 3 == 0; let m5 = i % 5 == 0;
if m3 || m5 {
println!("{}{}",
if m3 { "Fizz" } else { "" },
if m5 { "Buzz" } else { "" });
} else {
println!("{}", i);
}
}
}</code></pre>
<p>(Note: this isn’t the most idiomatic Rust.<br />
We’ll get there.)</p>
</section></section>
<section><section id="ownership-b" class="title-slide slide level1"><h1>Ownership <a href="https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html">[B]</a></h1><p>Ownership is Rust’s way of memory management.</p>
<h3>
Ownership rules
</h3>
<ul>
<li>Each <em>value</em> has exactly one binding that <em>owns</em> it.</li>
<li>When the binding goes out of scope, the value is <em>dropped</em>.</li>
</ul></section><section id="string" class="slide level2">
<h2><code>String</code></h2>
<p>To exemplify ownership, let’s look at the String type: a resizable string buffer.</p>
<p>It is akin to <code>StringBuilder</code> in Java/C#, or <code>std::string</code> in C++.</p>
<pre class="rs"><code>let mut s = String::from("hello");
s.push_str(", world!");
println!("{}", s);</code></pre>
<p>This prints “hello, world!”.</p>
</section><section id="string-1" class="slide level2">
<h2><code>String</code></h2>
<p><code>String</code> is internally a pointer to a heap buffer and a string length (plus the length of the buffer).</p>
<p>When we create a new <code>String</code>, the function we call allocates this buffer for us.</p>
<p>But how do we then deallocate (free) this buffer?</p>
</section><section id="dropping" class="slide level2">
<h2>Dropping</h2>
<pre class="rs"><code>{
let mut s = String::from("hello");
// do things with s
} // the String owned by s is dropped
</code></pre>
<p><code>s</code> owns the <code>String</code>. When <code>s</code> goes out of scope, the value it points to is dropped.</p>
<p>When a value is dropped, a destructor can be called. In the case of <code>String</code>, it frees the heap allocation.</p>
<p>This is essentially RAII in C++.</p>
</section><section id="moving" class="slide level2">
<h2>Moving</h2>
<pre class="rs"><code>{
let s1 = String::from("hello");
let s2 = s1;
}</code></pre>
<p>When <code>s1</code> and <code>s2</code> go out of scope, what happens?</p>
</section><section id="moving-1" class="slide level2">
<h2>Moving</h2>
<p>When we do <code>s2 = s1</code>, the <code>String</code> is <em>moved</em> into <code>s2</code>. <code>s1</code> is now invalid.</p>
<pre class="rs"><code>{
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s1);
// error[E0382]: borrow of moved value: `s1`
}</code></pre>
<p>This is what is unique about Rust: values have <em>unique ownership</em>; this eliminates use-after-frees and double-frees.</p>
</section><section id="cloneing" class="slide level2">
<h2><code>clone</code>ing</h2>
<pre class="rs"><code>let s1 = String::from("hello");
let s2 = s1.clone();
println!("{}", s1);</code></pre>
<p><code>clone</code> is a method implemented by <code>String</code> that makes a deep copy.</p>
</section><section id="what-about-primitives" class="slide level2">
<h2>What about primitives?</h2>
<pre class="rs"><code>let a = 1;
let b = a;
println!("{}", a);</code></pre>
<p>Scalar primitives (integers, floats, characters) implement the <code>Copy</code> trait.</p>
<p>(We’ll talk about traits later.)</p>
</section><section id="drop" class="slide level2">
<h2><a href="https://doc.rust-lang.org/std/mem/fn.drop.html"><code>drop</code></a></h2>
<p>It is possible to manually drop a value:</p>
<pre class="rs"><code>let s1 = String::from("hello");
drop(s1);</code></pre>
<p>Quick question: How is <code>drop</code> implemented?</p>
</section></section>
<section><section id="references" class="title-slide slide level1"><h1>References</h1><p>Suppose we want a function that does work on a <code>String</code>.</p>
<pre class="rs"><code>fn calc_len(x: String) -> usize {
x.len()
}
fn main() {
let x = String::from("hello");
println!("{}", calc_len(x));
println!("{}", x);
// error[E0382]: borrow of moved value: `x`
}</code></pre></section><section id="references-1" class="slide level2">
<h2>References</h2>
<pre class="rs"><code>fn calc_len(x: String) -> (String, usize) {
let len = x.len();
(x, len)
}
fn main() {
let x = String::from("hello");
let y = calc_len(x);
let x = y.0;
let len = y.1;
println!("{}", x);
println!("{}", len);
}</code></pre>
<p>Not very convenient.</p>
</section><section id="references-2" class="slide level2">
<h2>References</h2>
<p>Let’s pass in a <em>reference</em> instead.</p>
<pre class="rs"><code>fn calc_len(x: &String) -> usize {
x.len()
}
fn main() {
let x = String::from("hello");
println!("{}", calc_len(&x));
println!("{}", x);
}</code></pre>
</section><section id="mutable-references" class="slide level2">
<h2>Mutable references</h2>
<pre class="rs"><code>fn change(some_string: &String) {
some_string.push_str(", world");
// error[E0596]: cannot borrow `*some_string` as mutable,
// as it is behind a `&` reference
}
fn main() {
let s = String::from("hello");
change(&s);
}</code></pre>
<p>Normal references are <em>immutable</em>.</p>
</section><section id="mutable-references-1" class="slide level2">
<h2>Mutable references</h2>
<pre class="rs"><code>fn change(some_string: &mut String) {
some_string.push_str(", world");
}
fn main() {
let mut s = String::from("hello");
change(&mut s);
}</code></pre>
<p>We need to make a <em>mutable</em> reference.</p>
</section><section id="aside-docs" class="slide level2">
<h2>Aside: Docs</h2>
<p><a href="https://doc.rust-lang.org/std/string/struct.String.html#method.into_bytes"><code>String</code> docs</a></p>
<p>You can tell whether a method takes:</p>
<table>
<tbody>
<tr class="odd">
<td style="text-align: left;">the entire value</td>
<td style="text-align: left;"><code>fn myfn(self)</code></td>
</tr>
<tr class="even">
<td style="text-align: left;">a mutable reference</td>
<td style="text-align: left;"><code>fn myfn(&mut self)</code></td>
</tr>
<tr class="odd">
<td style="text-align: left;">a reference</td>
<td style="text-align: left;"><code>fn myfn(&self)</code></td>
</tr>
</tbody>
</table>
</section><section id="mutable-refs-are-exclusive" class="slide level2">
<h2>Mutable refs are exclusive</h2>
<pre class="rs"><code>fn main() {
let mut s = String::from("hello");
let mutref1 = &mut s;
let mutref2 = &mut s;
// error[E0499]: cannot borrow `s` as mutable
// more than once at a time
println!("{}", mutref1);
}</code></pre>
</section><section id="mutable-refs-are-exclusive-1" class="slide level2">
<h2>Mutable refs are exclusive</h2>
<pre class="rs"><code>fn main() {
let mut s = String::from("hello");
let mutref1 = &mut s;
let mut t = s;
// error[E0505]: cannot move out of `s`
// because it is borrowed
println!("{}", mutref1);
}</code></pre>
</section><section id="mutable-refs-are-exclusive-2" class="slide level2">
<h2>Mutable refs are exclusive</h2>
<pre class="rs"><code>fn main() {
let mut s = String::from("hello");
let mutref1 = &mut s;
let ref1 = &s;
// error[E0502]: cannot borrow `s` as immutable
// because it is also borrowed as mutable
println!("{}", mutref1);
}</code></pre>
</section><section id="mutable-refs-are-exclusive-3" class="slide level2">
<h2>Mutable refs are exclusive</h2>
<p>This solves <em>data races</em>.</p>
<p>It is possible to have aliasing and mutability using <a href="https://doc.rust-lang.org/std/cell/index.html"><code>Cell</code>/<code>RefCell</code></a>, or <a href="https://doc.rust-lang.org/std/sync/index.html"><code>Mutex</code>/<code>RwLock</code></a> for multithreaded scenarios.</p>
<p>These types do checks at runtime instead, in line with Rust’s philosophy: you opt-in to only what you need.</p>
</section></section>
<section><section id="slices" class="title-slide slide level1"><h1>Slices</h1><p>We’ve passed references to <code>String</code>s.<br />
What if we want a substring?</p>
<pre class="rs"><code>let s = String::from("hello world");
let hello: &str = &s[0..5];
let world = &s[6..11];
println!("{} {}", world.len(), world);</code></pre>
<p><code>&str</code> is a <em>fat pointer</em>: like <code>char *</code>, but it tracks the <em>length</em> too.</p></section><section id="string-literals" class="slide level2">
<h2>String literals</h2>
<p>The type of a string literal is <code>&str</code> *.</p>
<pre class="rs"><code>let s: &str = "Hello!";
println!("{} {}", s.len(), s);</code></pre>
<p>We can slice them too.</p>
<pre class="rs"><code>let s: &str = "Hello world!";
println!("{}", &s[..5]);</code></pre>
<p>* Almost.</p>
</section><section id="array-slices" class="slide level2">
<h2>Array slices</h2>
<p>You can slice arrays:</p>
<pre class="rs"><code>let arr: [u32; 5] = [1, 2, 3, 4, 5];
let slice: &[u32] = &arr[..3];
println!("{:?}", slice);</code></pre>
<p>The type of a slice is just <code>&[T]</code>.</p>
</section></section>
<section><section id="lifetimes" class="title-slide slide level1"><h1>Lifetimes</h1><pre class="rs" data-line-numbers=""><code>let r;
{
let x = 5;
r = &x;
// error[E0597]: `x` does not live long enough
}
println!("r: {}", r);</code></pre>
<p><code>x</code> lives from line 3–6: its lifetime ends at line 6.</p>
<p>Therefore, any reference to <code>x</code> cannot be referenced past line 6.</p>
<p>We’ve eliminated dangling pointers.</p></section><section id="specifying-lifetimes" class="slide level2">
<h2>Specifying lifetimes</h2>
<p>Lifetimes are part of references’ types.</p>
<p>It is possible to name lifetimes, but we’ll only see how this is important later.</p>
<p>There is a special lifetime name <code>static</code>.</p>
<p>With that, we can give the full type of a string literal:</p>
<pre class="rs"><code>let x: &'static str = "Hello!";</code></pre>
</section></section>
<section><section id="structs-b" class="title-slide slide level1"><h1>Structs <a href="https://doc.rust-lang.org/book/ch05-01-defining-structs.html">[B]</a></h1><pre class="rs"><code>struct Bus {
number: String, interval: u32
}
fn get_route(num: &str) -> Bus {
Bus {
number: String::from(num), interval: 5
}
}
fn main() {
let route = get_route("A1");
println!("Got route: {}, interval {}",
route.number, route.interval);
}</code></pre></section><section id="impling-methods" class="slide level2">
<h2><code>impl</code>ing methods</h2>
<p>Continuing from the previous example.</p>
<pre class="rs"><code>impl Bus {
fn arrive(&self) {
println!("{} has arrived", self.number);
}
}
fn main() {
let route = get_route("A1");
route.arrive();
}</code></pre>
</section><section id="update-syntax" class="slide level2">
<h2>Update syntax</h2>
<pre class="rs"><code>impl Bus {
fn with_number(self, new_num: &str) -> Bus {
Bus {
number: String::from(new_num),
..self
}
}
}
fn main() {
let route = get_route("A1").with_number("A2");
route.arrive();
}</code></pre>
</section><section id="associated-methods" class="slide level2">
<h2>Associated methods</h2>
<p>i.e. “static” methods</p>
<pre class="rs"><code>impl Bus {
fn of_number(num: &str) -> Self {
Bus {
number: String::from(num), interval: 5
}
}
}
fn main() {
let route = Bus::of_number("A1").with_number("A2");
route.arrive();
}</code></pre>
</section></section>
<section><section id="enums-b" class="title-slide slide level1"><h1>Enums <a href="https://doc.rust-lang.org/book/ch06-01-defining-an-enum.html">[B]</a></h1><p>Sum types, or algebraic data types.</p>
<pre class="rs"><code>enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}</code></pre></section><section id="using-enums" class="slide level2">
<h2>Using enums</h2>
<pre class="rs"><code>fn handle(x: Message) {
// ... ???
}
fn main() {
let x = Message::ChangeColor(0, 0, 0);
handle(x);
}</code></pre>
<p>How do we know which variant we have?</p>
</section></section>
<section><section id="match-expression-b" class="title-slide slide level1"><h1><code>match</code> expression <a href="https://doc.rust-lang.org/book/ch06-02-match.html">[B]</a></h1><pre class="rs"><code>let x = Message::ChangeColor(0, 0, 0);
fn handle(x: Message) {
match x {
Message::Quit => println!("Quitting."),
Message::Move { x, y } => println!("Moving to {}, {}", x, y),
Message::Write(str) => println!("Write {}", str),
Message::ChangeColor(r, g, b) => println!("Changing colour to {}, {}, {}", r, g, b)
};
}</code></pre></section><section id="match-is-exhaustive" class="slide level2">
<h2><code>match</code> is exhaustive</h2>
<pre class="rs"><code>fn handle(x: Message) {
match x {
Message::Quit => println!("Quitting."),
Message::Move { x, y } => println!("Moving to {}, {}", x, y),
Message::Write(str) => println!("Write {}", str),
// Message::ChangeColor(r, g, b) => println!("Changing colour to {}, {}, {}", r, g, b)
};
}</code></pre>
<p>Comment out that line. We get:</p>
<pre><code>error[E0004]: non-exhaustive patterns:
`ChangeColor(_, _, _)` not covered</code></pre>
</section></section>
<section id="if-let-b" class="title-slide slide level1"><h1><code>if let</code> <a href="https://doc.rust-lang.org/book/ch06-03-if-let.html">[B]</a></h1><p>If we only care about one case, we can use <code>if let</code>:</p>
<pre><code>let x = Message::ChangeColor(0, 0, 0);
if let Message::ChangeColor(r, g, b) = x {
println!("Changing colour to {}, {}, {}", r, g, b);
}</code></pre></section>
<section><section id="patterns-br" class="title-slide slide level1"><h1>Patterns <a href="https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html">[B]</a><a href="https://doc.rust-lang.org/stable/reference/patterns.html">[R]</a></h1><p><code>if let</code>, <code>match</code>, and in fact <code>let</code> all work on <em>patterns</em>.</p>
<pre class="rs"><code>let (x, y) = (1, 2);
println!("{} {}", x, y);</code></pre>
<p><code>(x, y)</code> is a <em>tuple pattern</em>.</p></section><section id="patterns" class="slide level2">
<h2>Patterns</h2>
<p>You can do the same with enums and structs.</p>
<pre class="rs"><code>struct Bus {
number: String, interval: u32
}
impl Bus {
fn of_number(num: &str) -> Self {
Bus {
number: String::from(num), interval: 5
}
}
}
fn main() {
let route = Bus::of_number("A1");
let Bus { interval: int, .. } = route;
println!("Got interval {}", int);
}</code></pre>
</section><section id="patterns-1" class="slide level2">
<h2>Patterns</h2>
<p><code>_</code> acts as a wildcard, since matches need to be exhaustive.</p>
<pre class="rs"><code>fn main() {
let x = 7;
match x {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
};
}</code></pre>
</section></section>
<section><section id="exercise-2" class="title-slide slide level1"><h1>Exercise 2</h1><p>Rewrite FizzBuzz with <code>match</code>.</p></section><section id="exercise-2-1" class="slide level2">
<h2>Exercise 2</h2>
<pre><code>fn main() {
for i in 1..=100 {
match (i % 3, i % 5) {
(0, 0) => println!("FizzBuzz"),
(0, _) => println!("Fizz"),
(_, 0) => println!("Buzz"),
(_, _) => println!("{}", i)
}
}
}</code></pre>
</section></section>
<section id="back-to-enums" class="title-slide slide level1"><h1>Back to enums</h1><p>Like structs, you can define methods on them.</p>
<pre class="rs"><code>enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
}
impl Message {
fn is_quit(&self) -> bool {
match self {
Message::Quit => true,
_ => false
}
}
}
fn main() {
println!("{}", Message::Quit.is_quit());
}</code></pre></section>
<section><section id="option" class="title-slide slide level1"><h1>Option</h1><p>Instead of <code>null</code>, Rust has the <code>Option</code> type.</p>
<pre class="rs"><code>struct Person {
name: Option<String>
}
impl Person {
fn greet(&self) {
match &self.name {
Some(name) => println!("Hello, {}", name),
None => println!("Hello, MISSINGNO.")
}
}
}
fn main() {
Person { name: None }.greet();
}</code></pre></section><section id="option-methods" class="slide level2">
<h2>Option methods</h2>
<p><code>Option</code> has many <a href="https://doc.rust-lang.org/std/option/enum.Option.html">methods</a>. Let’s rewrite <code>Person::greet</code>.</p>
<pre class="rs"><code>struct Person {
name: Option<String>
}
impl Person {
fn greet(&self) {
println!("Hello, {}",
self.name.as_ref().map(String::as_str)
.unwrap_or("MISSINGNO."));
}
}
fn main() {
Person { name: None }.greet();
}</code></pre>
</section></section>
<section id="result-b" class="title-slide slide level1"><h1>Result <a href="https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html">[B]</a></h1><p>Instead of exceptions, Rust has <code>Result</code>.</p>
<pre class="rs"><code>fn might_fail(inp: i32) -> Result<i32, &'static str> {
if inp % 3 == 0 {
Err("failed!")
} else {
Ok(inp)
}
}
fn main() {
match might_fail(3) {
Ok(result) => println!("Got {}", result),
Err(err) => println!("Failed: {}", err)
};
}</code></pre>
<p><code>Result</code> is a <em>monad</em>, and has many <a href="https://doc.rust-lang.org/std/result/enum.Result.html">methods</a> too.</p></section>
<section id="propagating-errors-b" class="title-slide slide level1"><h1>Propagating errors: <code>?</code> <a href="https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#propagating-errors">[B]</a></h1><p>The <code>?</code> operator helps to reduce boilerplate.</p>
<pre class="rs"><code>fn might_fail_2(inp: i32) -> Result<i32, &'static str> {
let x = might_fail(inp)?;
Ok(might_fail(2)? * x)
}
fn main() {
println!("{:?}", might_fail_2(3));
println!("{:?}", might_fail_2(4));
}</code></pre></section>
<section id="panicking-b" class="title-slide slide level1"><h1><code>panic</code>king <a href="https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html">[B]</a></h1><p>Some functions are documented to <code>panic</code> on failure conditions.</p>
<p>On <code>panic</code>, your application will abort.</p>
<p>It is Rust’s way of failing fast, when there is no way to recover from an exceptional situation.</p></section>
<section><section id="generics-b" class="title-slide slide level1"><h1>Generics <a href="https://doc.rust-lang.org/book/ch10-01-syntax.html">[B]</a></h1><p>Rust has generics, akin to those in Java and C#.</p>
<p>We’ve already seen <code>Option</code> and <code>Result</code>.<br />
Let’s see how they are defined.</p>
<pre class="rs"><code>enum Option<T> {
Some(T),
None,
}
enum Result<T, E> {
Ok(T),
Err(E),
}</code></pre></section><section id="generics" class="slide level2">
<h2>Generics</h2>
<p>In <code>impl</code>s:</p>
<pre class="rs"><code>struct Point<T> {
x: T,
y: T,
}
impl<T> Point<T> {
fn x(&self) -> &T {
&self.x
}
}</code></pre>
</section><section id="generics-1" class="slide level2">
<h2>Generics</h2>
<p>You can <code>impl</code> a specific concrete type:</p>
<pre class="rs"><code>impl Point<f32> {
fn distance_from_origin(&self) -> f32 {
(self.x.powi(2) + self.y.powi(2)).sqrt()
}
}</code></pre>
</section><section id="generics-over-lifetimes" class="slide level2">
<h2>Generics over lifetimes</h2>
<pre class="rs"><code>fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
if x.len() > y.len() {
x
} else {
y
}
}</code></pre>
</section></section>
<section><section id="traits" class="title-slide slide level1"><h1>Traits</h1><p>Traits are like interfaces in Java and C#. They’re closest to Haskell typeclasses.</p>
<pre class="rs"><code>trait Summary {
fn summarize(&self) -> String;
}</code></pre></section><section id="impling-traits-b" class="slide level2">
<h2><code>impl</code>ing traits <a href="https://doc.rust-lang.org/book/ch10-02-traits.html#implementing-a-trait-on-a-type">[B]</a></h2>
<pre class="rs"><code>struct NewsArticle {
headline: String,
author: String,
content: String,
}
impl Summary for NewsArticle {
fn summarize(&self) -> String {
format!("{}, by {}", self.headline, self.author)
}
}
fn main() {
let art = NewsArticle {
headline: "Rust is great!".to_owned(),
author: "Graydon Hoare".to_owned(),
content: "Rust is really really good!".to_owned()
};
println!("{}", art.summarize());
}</code></pre>
</section><section id="impling-traits" class="slide level2">
<h2><code>impl</code>ing traits</h2>
<p>You can <code>impl</code> a trait you define on other types:</p>
<pre class="rs"><code>impl Summary for String {
fn summarize(&self) -> String {
(&self[..self.len().min(10)]).to_owned()
}
}
fn main() {
let x = String::from("Hellohello hello");
println!("{}", x.summarize());
}</code></pre>
</section><section id="trait-bounds-b" class="slide level2">
<h2>Trait bounds <a href="https://doc.rust-lang.org/book/ch10-02-traits.html#trait-bound-syntax">[B]</a></h2>
<pre class="rs"><code>fn print_summary<T>(x: &T) where T: Summary {
println!("{}", x.summarize());
}
fn main() {
let x = String::from("Hellohello hello");
print_summary(&x);
}</code></pre>
</section><section id="trait-bounds" class="slide level2">
<h2>Trait bounds</h2>
<pre class="rs"><code>fn print_summary<T: Summary>(x: &T) {
println!("{}", x.summarize());
}
fn main() {
let x = String::from("Hellohello hello");
print_summary(&x);
}</code></pre>
</section><section id="blanket-impls-b" class="slide level2">
<h2>Blanket <code>impl</code>s <a href="https://doc.rust-lang.org/book/ch10-02-traits.html#using-trait-bounds-to-conditionally-implement-methods">[B]</a></h2>
<pre class="rs"><code>use std::fmt::Display;
impl<T: Display> Summary for T {
fn summarize(&self) -> String {
let s = self.to_string();
(&s[..s.len().min(5)]).to_owned()
}
}
fn main() {
print_summary(&1234567890);
}</code></pre>
</section></section>
<section><section id="common-traits" class="title-slide slide level1"><h1>Common traits</h1><ul>
<li><code>Debug</code>: Can be printed with <code>{:?}</code></li>
<li><code>Clone</code>: Can be <code>.clone()</code>d</li>
<li><code>Copy</code>: Opt-out of move semantics</li>
</ul></section><section id="derive" class="slide level2">
<h2><code>#[derive]</code></h2>
<p>Some traits can be auto-derived:</p>
<pre class="rs"><code>#[derive(Debug, Clone)]
struct Bus {
number: String, interval: u32
}
fn main() {
let a2 = Bus {
number: "A2".to_owned(),
interval: 5
};
let a2_2 = a2.clone();
println!("{:?}", a2);
println!("{:?}", a2_2);
}</code></pre>
</section></section>
<section id="operators" class="title-slide slide level1"><h1>Operators</h1><p>Rust operators are defined using traits e.g. <a href="https://doc.rust-lang.org/std/ops/trait.Add.html"><code>Add</code></a></p>
<pre class="rs"><code>use std::ops::Add;
#[derive(Debug, Clone, Copy)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
fn main() {
let p1 = Point { x: 5, y: 5 };
let p2 = Point { x: -5, y: 5 };
println!("{:?}", p1 + p2);
}</code></pre></section>
<section><section id="lambdas-and-closures-b" class="title-slide slide level1"><h1>Lambdas and closures <a href="https://doc.rust-lang.org/book/ch13-01-closures.html">[B]</a></h1><pre class="rs"><code>fn add_one_v1 (x: u32) -> u32 { x + 1 }
let add_one_v2 = |x: u32| -> u32 { x + 1 };
let add_one_v3 = |x| { x + 1 };
let add_one_v4 = |x| x + 1 ;</code></pre></section><section id="lambdas-and-closures" class="slide level2">
<h2>Lambdas and closures</h2>
<p>Closures implement (one of)</p>
<ul>
<li><a href="https://doc.rust-lang.org/std/ops/trait.Fn.html"><code>Fn</code></a>: those that only take immutable references to captured variables</li>
<li><a href="https://doc.rust-lang.org/std/ops/trait.FnMut.html"><code>FnMut</code></a>: those that take mutable references</li>
<li><a href="https://doc.rust-lang.org/std/ops/trait.FnOnce.html"><code>FnOnce</code></a>: those that might only be callable once</li>
</ul>
</section><section id="lambdas-and-closures-1" class="slide level2">
<h2>Lambdas and closures</h2>
<pre class="rs"><code>fn apply_fn<F, T>(f: F, arg: T) -> T
where F: Fn(T) -> T {
f(arg)
}
fn main() {
println!("{}", apply_fn(|x| x * 2, 5));
}</code></pre>
</section><section id="lambdas-and-closures-2" class="slide level2">
<h2>Lambdas and closures</h2>
<pre class="rs"><code>fn apply_fn<F, T>(mut f: F, arg: T) -> T
where F: FnMut(T) -> T {
f(arg)
}
fn main() {
let mut y = 10;
println!("{}", apply_fn(|x| { y += x; y }, 5));
}</code></pre>
</section></section>
<section><section id="iterators-b" class="title-slide slide level1"><h1>Iterators <a href="https://doc.rust-lang.org/book/ch13-02-iterators.html">[B]</a></h1><p>Iterators are like Streams in Java, and LINQ in C#.</p>
<p>You can iterate through them:</p>
<pre class="rs"><code>let x = [1, 2, 3, 4, 5];
for i in x.iter() {
println!("{}", i);
}</code></pre></section><section id="iterators" class="slide level2">
<h2>Iterators</h2>
<p>But their power comes from <a href="https://doc.rust-lang.org/std/iter/trait.Iterator.html">their composability</a>:</p>
<pre class="rs"><code>let x = [1, 2, 3, 4, 5];
for i in x.iter().map(|x| x * x) {
println!("{}", i);
}</code></pre>
</section><section id="iterators-1" class="slide level2">
<h2>Iterators</h2>
<p>On ranges:</p>
<pre class="rs"><code>for i in (1..=5).map(|x| x * x) {
println!("{}", i);
}</code></pre>
</section><section id="iterators-2" class="slide level2">
<h2>Iterators</h2>
<pre class="rs"><code>for i in (1..=5).chain(21..=25).map(|x| x * x) {
println!("{}", i);
}</code></pre>
</section><section id="iterators-3" class="slide level2">
<h2>Iterators</h2>
<pre class="rs"><code>let x = ["Hello", "World", "3", "4", "5"];
for (i, s) in x.iter().enumerate() {
println!("{} = {}", i, s);
}</code></pre>
</section></section>
<section><section id="crates.io" class="title-slide slide level1"><h1><a href="https://crates.io/">crates.io</a></h1><p>Rust’s package repository.</p></section><section id="adding-dependencies" class="slide level2">
<h2>Adding dependencies</h2>
<p>In <code>Cargo.toml</code>:</p>
<pre class="toml"><code>[dependencies]
rand = "0.7"</code></pre>