-
Notifications
You must be signed in to change notification settings - Fork 1
/
iwaco-2017.html
778 lines (763 loc) · 75.7 KB
/
iwaco-2017.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<meta name="author" content="Felix Klock (@pnkfelix), Mozilla">
<title>Aliasing, Capabilities, and Ownership in Rust</title>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="reveal.js/css/theme/mozilla-sandstone.css" id="theme">
<link rel="stylesheet" href="slide-style.css"/>
<link rel="stylesheet" href="code-style.css"/>
<link rel="stylesheet" href="fonts.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 ) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="reveal.js/lib/js/html5shiv.js"></script>
<![endif]-->
<script src="snap.svg-min.js"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1 class="title">Aliasing, Capabilities, and Ownership in Rust</h1>
<p class="author">Felix Klock (<code>@pnkfelix</code>), Mozilla</p>
<p class="date"><kbd class="key">space</kbd>: next slide; <kbd class="key">shift</kbd> <kbd class="key">space</kbd>: prev slide; <kbd class="key">esc</kbd>: overview; arrows navigate <a href="http://pnkfx.org/presentations/iwaco-2017.html"><code>http://bit.ly/2rM9w0r</code></a></p>
</section>
<section id="what-is-rust" class="slide level2">
<h2>What is Rust?</h2>
<h3 id="new-programming-language">New programming language</h3>
<ul>
<li class="fragment">(... and ecosystem, and development community)</li>
</ul>
<h3 id="emphasizing-control-safety-and-speed">Emphasizing control, safety, and speed</h3>
<ul>
<li class="fragment">(... <em>especially</em> when it comes to concurrency)</li>
</ul>
<h3 id="low-level-hacking-without-fear-of-segfaults-nor-data-races">Low-level hacking without fear of segfaults nor data races</h3>
<ul>
<li class="fragment">or more simply: "Hack without Fear"</li>
</ul>
</section>
<section id="why-..." class="slide level2">
<h2>Why ...?</h2>
</section>
<section id="why-use-rust" class="slide level2 big_text" data-transition="fade-out">
<h2>Why use Rust?</h2>
<ul>
<li class="fragment">Fast code, low memory footprint</li>
<li class="fragment">Go from bare metal (assembly; C FFI) ...
<div class="fragment">
... to high-level (closures, generic containers) ...
</div>
<div class="fragment">
with <em>zero cost</em> (no GC, unboxed closures, monomorphization); no runtime, cross-lang interop
</div></li>
<li class="fragment"><em>Safety</em> and <em>Parallelism</em></li>
</ul>
</section>
<section id="safety-and-parallelism" class="slide level2 center">
<h2>Safety and Parallelism</h2>
<h3 id="safety">Safety</h3>
<ul>
<li><p>No segmentation faults</p></li>
<li><p>No undefined behavior</p></li>
<li><p>No data races</p></li>
</ul>
<h3 id="multi-paradigm-parallelism">(Multi-paradigm) Parallelism</h3>
<ul>
<li><p>msg passing via channels</p></li>
<li><p>shared state (R/W-capabilities controlled via types)</p></li>
<li><p>use native threads... or scoped threads... or work-stealing...</p></li>
</ul>
</section>
<section id="algebraic-data-types" class="slide level2">
<h2>Algebraic Data Types</h2>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">enum</span> BinaryTree <span class="op">{</span> Leaf(<span class="dt">i32</span>), Node(<span class="dt">Box</span><BinaryTree>, <span class="dt">i32</span>, <span class="dt">Box</span><BinaryTree>) <span class="op">}</span>
<span class="kw">fn</span> sample_tree() -> BinaryTree <span class="op">{</span>
<span class="kw">let</span> l1 = <span class="dt">Box</span>::new(BinaryTree::Leaf(<span class="dv">1</span>));
<span class="kw">let</span> l3 = <span class="dt">Box</span>::new(BinaryTree::Leaf(<span class="dv">3</span>));
<span class="kw">let</span> n2 = <span class="dt">Box</span>::new(BinaryTree::Node(l1, <span class="dv">2</span>, l3));
<span class="kw">let</span> l5 = <span class="dt">Box</span>::new(BinaryTree::Leaf(<span class="dv">5</span>));
BinaryTree::Node(n2, <span class="dv">4</span>, l5) <span class="op">}</span></code></pre></div>
<div class="fragment">
<svg height="280" baseProfile="full" viewBox="0 0 621 280" width="621" version="1.1" xmlns="http://www.w3.org/2000/svg">
<path fill="none" stroke-width="2" d="M 526.5,14 Q 526.5,7 531,7Q 531,7 540,7L 549,7L 558,7Q 562.5,7 562.5,14L 562.5,28Q 562.5,35 558,35Q 558,35 549,35L 540,35L 531,35Q 526.5,35 526.5,28L 526.5,14 Z" stroke="green" />
<path d="M 472.5,56 Q 472.5,49 477,49Q 477,49 486,49L 490.5,49L 504,49Q 508.5,49 508.5,56L 508.5,70Q 508.5,77 504,77Q 504,77 495,77L 486,77L 477,77Q 472.5,77 472.5,70L 472.5,56 Z" stroke-width="2" fill="none" stroke="green" />
<path stroke-width="2" stroke="green" d="M 580.5,49L 594,49L 598.5,49L 612,49L 616.5,49L 616.5,70L 616.5,77L 603,77L 594,77L 585,77L 580.5,77L 580.5,56 Z" fill="none" />
<path d="M 436.5,91L 450,91L 454.5,91L 468,91L 472.5,91L 472.5,112L 472.5,119L 459,119L 450,119L 441,119L 436.5,119L 436.5,98 Z" fill="none" stroke-width="2" stroke="green" />
<path d="M 517.5,91L 531,91L 535.5,91L 549,91L 553.5,91L 553.5,112L 553.5,119L 540,119L 531,119L 522,119L 517.5,119L 517.5,98 Z" fill="none" stroke-width="2" stroke="green" />
<path fill="none" d="M 283.5,154 Q 283.5,147 288,147Q 288,147 297,147L 306,147L 315,147L 324,147L 333,147L 342,147Q 346.5,147 346.5,154L 346.5,168L 346.5,182L 346.5,196L 346.5,210L 346.5,224L 346.5,238Q 346.5,245 342,245Q 342,245 333,245L 324,245L 315,245L 306,245L 297,245L 288,245Q 283.5,245 283.5,238L 283.5,224L 283.5,210L 283.5,196L 283.5,182L 283.5,168L 283.5,154 Z" stroke-width="2" stroke="green" />
<path d="M 400.5,154 Q 400.5,147 405,147Q 405,147 414,147L 423,147L 432,147L 441,147L 450,147L 459,147Q 463.5,147 463.5,154L 463.5,168L 463.5,182L 463.5,196L 463.5,210L 463.5,224L 463.5,238Q 463.5,245 459,245Q 459,245 450,245L 441,245L 432,245L 423,245L 414,245L 405,245Q 400.5,245 400.5,238L 400.5,224L 400.5,210L 400.5,196L 400.5,182L 400.5,168L 400.5,154 Z" fill="none" stroke-width="2" stroke="green" />
<path fill="none" stroke-width="2" stroke="green" d="M 517.5,154 Q 517.5,147 522,147Q 522,147 531,147L 540,147L 549,147L 558,147L 567,147L 576,147Q 580.5,147 580.5,154L 580.5,168L 580.5,182L 580.5,196L 580.5,210L 580.5,224L 580.5,238Q 580.5,245 576,245Q 576,245 567,245L 558,245L 549,245L 540,245L 531,245L 522,245Q 517.5,245 517.5,238L 517.5,224L 517.5,210L 517.5,196L 517.5,182L 517.5,168L 517.5,154 Z" />
<path stroke-width="2" d="M 490.5,49L 490.5,28Q 490.5,21 495,21Q 495,21 504,21L 513,21L 522,21" fill="none" stroke="green" />
<path d="M 567,21 L 576,21L 585,21L 594,21Q 598.5,21 598.5,28L 598.5,42L 598.5,49" stroke-width="2" stroke="green" fill="none" />
<path fill="none" d="M 454.5,91L 454.5,70Q 454.5,63 459,63Q 459,63 468,63" stroke-width="2" stroke="green" />
<path stroke-width="2" fill="none" stroke="green" d="M 513,63 L 522,63L 531,63Q 535.5,63 535.5,70L 535.5,84L 535.5,91" />
<path d="M 85.5,224 Q 85.5,231 81,231Q 81,231 72,231L 63,231L 54,231L 45,231L 36,231L 27,231Q 22.5,231 22.5,224L 22.5,210L 22.5,196L 22.5,182L 22.5,168L 22.5,154L 22.5,140Q 22.5,133 27,133Q 27,133 36,133L 45,133L 54,133L 63,133L 72,133L 81,133Q 85.5,133 85.5,140L 85.5,154" stroke="green" fill="none" stroke-width="2" />
<path stroke-width="2" d="M 229.5,238 Q 229.5,245 225,245Q 225,245 216,245L 207,245L 198,245L 189,245L 180,245L 171,245Q 166.5,245 166.5,238L 166.5,224L 166.5,210L 166.5,196L 166.5,182L 166.5,168L 166.5,154Q 166.5,147 171,147Q 171,147 180,147L 189,147L 198,147L 207,147L 216,147L 225,147Q 229.5,147 229.5,154L 229.5,168" fill="none" stroke="green" />
<path stroke-width="2" fill="none" stroke="green" d="M 72,161 L 81,161L 90,161L 99,161L 108,161L 117,161L 126,161L 135,161L 144,161L 153,161L 157.5,161 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" />
<path stroke-width="2" d="M 216,175 L 225,175L 234,175L 243,175L 247.5,175Q 256.5,161 261,161Q 261,161 270,161L 274.5,161 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" stroke="green" fill="none" />
<path stroke-width="2" d="M 216,231 L 225,231L 234,231L 243,231L 252,231Q 256.5,231 256.5,238L 256.5,252Q 256.5,259 261,259Q 261,259 270,259L 279,259L 288,259L 297,259L 306,259L 315,259L 324,259L 333,259L 342,259L 351,259L 360,259L 369,259Q 373.5,259 373.5,252L 373.5,238L 373.5,224L 373.5,210L 373.5,196L 373.5,182L 373.5,168Q 373.5,161 378,161Q 378,161 387,161L 391.5,161 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" fill="none" stroke="green" />
<path stroke-width="2" stroke="green" fill="none" d="M 72,217 L 81,217L 90,217L 99,217L 108,217L 117,217Q 121.5,217 121.5,224L 121.5,238L 121.5,252L 121.5,266Q 121.5,273 126,273Q 126,273 135,273L 144,273L 153,273L 162,273L 171,273L 180,273L 189,273L 198,273L 207,273L 216,273L 225,273L 234,273L 243,273L 252,273L 261,273L 270,273L 279,273L 288,273L 297,273L 306,273L 315,273L 324,273L 333,273L 342,273L 351,273L 360,273L 369,273L 378,273L 387,273L 396,273L 405,273L 414,273L 423,273L 432,273L 441,273L 450,273L 459,273L 468,273L 477,273L 486,273Q 490.5,273 490.5,266L 490.5,252L 490.5,238L 490.5,224L 490.5,210L 490.5,196L 490.5,182L 490.5,168Q 490.5,161 495,161Q 495,161 504,161L 508.5,161 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" />
<path stroke="green" fill="none" stroke-width="2" d="M 85.5,168 L 85.5,182L 85.5,196L 85.5,210" />
<path fill="none" d="M 229.5,182 L 229.5,196L 229.5,210L 229.5,224" stroke="green" stroke-width="2" />
<text font-family="Menlo" font-size="14" dominant-baseline="middle" text-anchor="start" x="0" y="7" fill="black" xml:space="preserve">CONCRETE: ABSTRACT: </text>
<text fill="black" font-family="Menlo" font-size="14" text-anchor="start" xml:space="preserve" dominant-baseline="middle" x="540" y="21">4 </text>
<text font-family="Menlo" y="63" dominant-baseline="middle" text-anchor="start" x="0" fill="black" font-size="14" xml:space="preserve">(STACK) (HEAP) </text>
<text font-size="14" text-anchor="start" dominant-baseline="middle" xml:space="preserve" font-family="Menlo" x="486" fill="black" y="63">2 </text>
<text font-family="Menlo" font-size="14" y="63" text-anchor="start" x="594" fill="black" xml:space="preserve" dominant-baseline="middle">5 </text>
<text font-family="Menlo" xml:space="preserve" font-size="14" y="105" text-anchor="start" dominant-baseline="middle" fill="black" x="0">sample_tree: </text>
<text text-anchor="start" dominant-baseline="middle" font-family="Menlo" font-size="14" fill="black" y="105" xml:space="preserve" x="450">1 </text>
<text font-family="Menlo" font-size="14" xml:space="preserve" dominant-baseline="middle" y="105" x="531" text-anchor="start" fill="black">3 </text>
<text xml:space="preserve" y="147" dominant-baseline="middle" font-family="Menlo" fill="black" text-anchor="start" font-size="14" x="27">Node: </text>
<text font-family="Menlo" x="36" font-size="14" xml:space="preserve" y="161" text-anchor="start" dominant-baseline="middle" fill="black">Box </text>
<text xml:space="preserve" y="161" font-family="Menlo" fill="black" font-size="14" text-anchor="start" x="171" dominant-baseline="middle">Node: </text>
<text font-size="14" x="288" font-family="Menlo" text-anchor="start" fill="black" xml:space="preserve" y="161" dominant-baseline="middle">Leaf: </text>
<text x="405" dominant-baseline="middle" xml:space="preserve" font-size="14" text-anchor="start" y="161" font-family="Menlo" fill="black">Leaf: </text>
<text dominant-baseline="middle" x="522" xml:space="preserve" y="161" font-family="Menlo" text-anchor="start" font-size="14" fill="black">Leaf: </text>
<text xml:space="preserve" text-anchor="start" dominant-baseline="middle" y="175" x="180" font-size="14" fill="black" font-family="Menlo">Box </text>
<text text-anchor="start" fill="black" xml:space="preserve" font-size="14" dominant-baseline="middle" y="189" font-family="Menlo" x="36">(4) </text>
<text dominant-baseline="middle" fill="black" x="297" font-family="Menlo" text-anchor="start" xml:space="preserve" y="189" font-size="14">(1) </text>
<text text-anchor="start" font-family="Menlo" x="414" y="189" fill="black" dominant-baseline="middle" font-size="14" xml:space="preserve">(3) </text>
<text y="189" dominant-baseline="middle" fill="black" xml:space="preserve" text-anchor="start" font-size="14" x="531" font-family="Menlo">(5) </text>
<text fill="black" text-anchor="start" xml:space="preserve" font-family="Menlo" font-size="14" x="180" dominant-baseline="middle" y="203">(2) </text>
<text text-anchor="start" xml:space="preserve" dominant-baseline="middle" fill="black" y="217" x="36" font-size="14" font-family="Menlo">Box </text>
<text font-size="14" dominant-baseline="middle" x="180" fill="black" y="231" text-anchor="start" xml:space="preserve" font-family="Menlo">Box </text>
</svg>
<ul>
<li class="fragment">Syntax: <code class="sourceCode rust"><span class="kw">enum</span></code> declares an algebraic (sum-of-product) data type</li>
<li class="fragment"><code class="sourceCode rust"><span class="dt">Box</span><T></code> is an <em>owning</em> reference to heap-allocated data of type <code class="sourceCode rust">T</code></li>
<li class="fragment">Boxing of values is explicit (+ all variants of an enum have same size)</li>
</ul>
</div>
</section>
<section id="ownership" class="slide level2">
<h2>Ownership</h2>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">enum</span> BinaryTree <span class="op">{</span> Leaf(<span class="dt">i32</span>), Node(<span class="dt">Box</span><BinaryTree>, <span class="dt">i32</span>, <span class="dt">Box</span><BinaryTree>) <span class="op">}</span></code></pre></div>
<ul>
<li class="fragment"><code class="sourceCode rust"><span class="kw">match</span></code> <em>moves</em> input to <code class="sourceCode rust">Leaf(n)</code> or <code class="sourceCode rust">Node(l, n, r)</code> as appropriate</li>
<li class="fragment"><code class="sourceCode rust">*expr</code> dereferences; here, moves tree <em>out</em> of box to local stack slot</li>
<li class="fragment"><code class="sourceCode rust"><span class="kw">fn</span></code> signature: pass-by-value + move semantics = args consumed</li>
</ul>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> tree_weight(t: BinaryTree) -> <span class="dt">i32</span> <span class="op">{</span>
<span class="kw">match</span> t <span class="op">{</span>
BinaryTree::Leaf(payload) => payload,
BinaryTree::Node(left, payload, right) => <span class="op">{</span>
tree_weight(*left) + payload + tree_weight(*right)
<span class="op">}</span>
<span class="op">}</span>
<span class="op">}</span></code></pre></div>
</section>
<section id="arguments-consumed" class="slide level2">
<h2>Arguments Consumed</h2>
<div class="sourceCode"><pre class="sourceCode rust compile_error"><code class="sourceCode rust"><span class="kw">fn</span> tree_weight(t: BinaryTree) -> <span class="dt">i32</span> <span class="op">{</span> ... <span class="op">}</span>
<span class="kw">fn</span> consume_semantics() <span class="op">{</span>
<span class="kw">let</span> t = sample_tree();
<span class="kw">let</span> w1 = tree_weight(t);
<span class="kw">let</span> w2 = tree_weight(t);
<span class="pp">println!</span>(<span class="st">"w1: {} w2: {}"</span>, w1, w2);
<span class="op">}</span></code></pre></div>
<div class="fragment">
<pre class="terminal"><code>error[E0382]: use of moved value: `t`
--> src/a00.rs:67:17
|
66 | tree_weight(t);
| - value moved here
67 | tree_weight(t);
| ^ value used here after move
|
= note: move occurs because `t` has type `BinaryTree`,
which does not implement the `Copy` trait</code></pre>
</div>
</section>
<section id="sending-resources-stdthread" class="slide level2">
<h2>Sending resources (<code class="sourceCode rust">std::thread</code>)</h2>
<!--
```rust
use std::rc::Rc;
use std::thread;
use std::cell::{Cell, RefCell};
#[test]
fn demo_send1() {
```
-->
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">let</span> (tx, rx) = channel(); <span class="co">// create transmitter/receiver pair</span>
thread::spawn(<span class="kw">move</span> || <span class="kw">match</span> rx.recv() <span class="op">{</span> <span class="co">// spawn A1, taking ownership of `rx`</span>
<span class="cn">Ok</span>(v_end) => <span class="op">{</span>
<span class="pp">println!</span>(<span class="st">"A1 received {:?}"</span>, v_end)
<span class="op">}</span> <span class="co">// (end of scope for `v_end`; automatic clean up.)</span>
<span class="cn">Err</span>(err) => <span class="pp">println!</span>(<span class="st">"A1 receive failure {:?}"</span>, err),
<span class="op">}</span>);
<span class="kw">let</span> t1 = tx.clone();
thread::spawn(<span class="kw">move</span> || <span class="op">{</span> <span class="co">// spawn A2, taking ownership of `t1`</span>
<span class="kw">let</span> <span class="kw">mut</span> v = <span class="dt">Vec</span>::new(); v.push(<span class="ch">'a'</span>); v.push(<span class="ch">'b'</span>); v.push(<span class="ch">'c'</span>);
t1.send(v).expect(<span class="st">"A2 send failure"</span>);
<span class="op">}</span>);
thread::spawn(<span class="kw">move</span> || <span class="op">{</span> <span class="co">// spawn A3, taking ownership of `tx`</span>
<span class="kw">let</span> v = <span class="pp">vec!</span><span class="op">[</span><span class="ch">'1'</span>, <span class="ch">'2'</span>, <span class="ch">'3'</span><span class="op">]</span>;
tx.send(v).expect(<span class="st">"A3 send failure"</span>);
<span class="op">}</span>);</code></pre></div>
<!--
```rust
}
```
-->
<ul>
<li class="fragment">Syntax: <code class="sourceCode rust">|arg, ...| expr</code> (incl. <code class="sourceCode rust">|| <span class="op">{</span> expr; ... <span class="op">}</span></code>) are <em>closures</em></li>
<li class="fragment"><code class="sourceCode rust"><span class="kw">move</span> |arg, ...| expr</code> captures by ownership xfer ("moving")</li>
<li class="fragment"><code class="sourceCode rust"><span class="pp">println!</span></code> and <code class="sourceCode rust"><span class="pp">vec!</span></code> are macros (typesafe printf; Vec building sugar)</li>
<li class="fragment">Destruction 1: at scope end for <code class="sourceCode rust">v_end</code>, buffer + contents reclaimed</li>
<li class="fragment">Destruction 2: at scope end for <code class="sourceCode rust">rx</code>, <code class="sourceCode rust">t<span class="dv">1</span></code> + <code class="sourceCode rust">tx</code>, channel reclaimed</li>
</ul>
</section>
<section id="sending-between-threads" class="slide level2">
<h2>Sending between threads</h2>
<svg height="420" xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 738 420" width="738" baseProfile="full">
<path fill="none" stroke="green" d="M 4.5,0 L 4.5,14L 4.5,28L 4.5,42L 4.5,49L 4.5,70L 4.5,84L 4.5,98L 4.5,112L 4.5,126L 4.5,133L 4.5,154L 4.5,168L 4.5,182L 4.5,196L 4.5,210L 4.5,217L 4.5,238L 4.5,252L 4.5,266L 4.5,280L 4.5,294L 4.5,308L 4.5,322L 4.5,336L 4.5,350L 4.5,364L 4.5,378L 4.5,392L 4.5,406L 4.5,420" stroke-width="2" />
<path id="txa" stroke-width="2" stroke-dasharray="5,2" d="M 22.5,14 L 22.5,28L 22.5,42L 22.5,56Q 22.5,63 27,63Q 27,63 36,63L 45,63L 54,63L 63,63Q 67.5,63 67.5,70L 67.5,84" fill="none" stroke="blue" />
<path id="move_rx" stroke="orange" d="M 67.5,14 Q 67.5,21 72,21Q 72,21 81,21L 90,21L 99,21L 108,21L 117,21L 126,21L 135,21L 144,21L 153,21L 162,21L 171,21L 180,21L 189,21L 198,21L 207,21L 216,21L 225,21L 234,21L 243,21L 252,21L 261,21L 270,21L 279,21L 288,21L 297,21L 306,21L 315,21L 324,21L 333,21L 342,21L 351,21L 360,21L 369,21L 378,21L 387,21L 396,21L 405,21L 414,21L 423,21L 432,21L 441,21L 450,21L 459,21L 468,21L 477,21Q 481.5,21 481.5,28L 481.5,42" stroke-width="10" fill="none" />
<path stroke="green" d="M 4.5,49L 18,49" fill="none" stroke-width="2" />
<path d="M 27,49 L 36,49L 45,49L 54,49L 63,49L 72,49L 81,49L 90,49L 99,49L 108,49L 117,49L 126,49L 135,49L 144,49L 153,49L 162,49L 171,49L 180,49L 189,49L 198,49L 207,49L 216,49L 225,49L 234,49L 243,49L 252,49L 261,49L 270,49L 279,49L 288,49L 297,49L 306,49L 315,49L 324,49L 333,49L 342,49L 351,49L 360,49L 369,49L 378,49L 387,49L 396,49L 405,49L 414,49L 423,49L 432,49L 441,49Q 445.5,49 445.5,56L 445.5,70L 445.5,84L 445.5,98L 445.5,112L 445.5,126L 445.5,140L 445.5,154L 445.5,168L 445.5,182L 445.5,196L 445.5,210L 445.5,224L 445.5,238L 445.5,252L 445.5,266L 445.5,280L 445.5,294L 445.5,308L 445.5,322L 445.5,336" fill="none" stroke="green" stroke-width="2" />
<path d="M 481.5,56 L 481.5,70L 481.5,84L 481.5,98L 481.5,112L 481.5,126L 481.5,140L 481.5,154L 481.5,168L 481.5,182L 481.5,196L 481.5,210L 481.5,224L 481.5,238L 481.5,252L 481.5,259L 495,280L 504,294L 513,308L 522,322L 531,336L 540,350L 544.5,357L 544.5,378L 544.5,392" stroke-width="2" id="rxa" stroke="blue" fill="none" stroke-dasharray="5,2" />
<path stroke="orange" stroke-width="10" fill="none" d="M 31.5,98 Q 31.5,105 36,105Q 36,105 45,105L 54,105L 63,105L 72,105L 81,105L 90,105L 99,105L 108,105L 117,105L 126,105L 135,105L 144,105L 153,105L 162,105L 171,105L 180,105L 189,105L 198,105L 207,105L 216,105L 225,105L 234,105L 243,105L 252,105L 261,105L 270,105L 279,105L 288,105L 297,105L 306,105L 315,105L 324,105Q 328.5,105 328.5,112L 328.5,126" id="m_t1" />
<path stroke-dasharray="5,2" stroke="blue" id="txb" d="M 67.5,112 L 67.5,126L 67.5,140L 67.5,154L 67.5,168" fill="none" stroke-width="2" />
<path stroke-width="2" stroke="green" d="M 4.5,133L 18,133L 27,133L 36,133L 45,133L 54,133L 63,133" fill="none" />
<path stroke-width="2" fill="none" stroke="green" d="M 72,133 L 81,133L 90,133L 99,133L 108,133L 117,133L 126,133L 135,133L 144,133L 153,133L 162,133L 171,133L 180,133L 189,133L 198,133L 207,133L 216,133L 225,133L 234,133L 243,133L 252,133L 261,133L 270,133L 279,133L 288,133L 297,133L 306,133Q 310.5,133 310.5,140L 310.5,154L 310.5,168L 310.5,182L 310.5,196L 310.5,210L 310.5,224L 310.5,238L 310.5,252L 310.5,266L 310.5,280L 310.5,294L 310.5,308L 310.5,322L 310.5,336L 310.5,350L 310.5,364" />
<path id="t1a" stroke-dasharray="5,2" fill="none" stroke-width="2" d="M 328.5,140 L 328.5,154L 328.5,168L 328.5,182L 328.5,196L 328.5,210L 328.5,224L 328.5,238L 328.5,252L 328.5,266L 328.5,280L 328.5,294L 328.5,308" stroke="blue" />
<path stroke="orange" stroke-width="10" id="m_t2" fill="none" d="M 76.5,182 Q 76.5,189 81,189Q 81,189 90,189L 99,189L 108,189L 117,189L 126,189L 135,189L 144,189L 153,189L 162,189L 171,189L 180,189L 189,189Q 193.5,189 193.5,196L 193.5,210" />
<path stroke="green" d="M 4.5,217L 18,217L 27,217L 36,217L 45,217L 54,217L 63,217L 72,217L 81,217L 90,217L 99,217L 108,217L 117,217L 126,217L 135,217L 144,217L 153,217L 162,217L 171,217Q 175.5,217 175.5,224L 175.5,238L 175.5,252L 175.5,266L 175.5,280L 175.5,294L 175.5,308L 175.5,322L 175.5,336L 175.5,350L 175.5,364L 175.5,378L 175.5,392L 175.5,406L 175.5,420" stroke-width="2" fill="none" />
<path stroke-width="2" fill="none" id="t2a" stroke="blue" d="M 193.5,224 L 193.5,238L 193.5,252L 193.5,266L 193.5,280L 193.5,294L 193.5,308" stroke-dasharray="5,2" />
<path stroke-width="10" stroke="orange" fill="none" id="m_v2" d="M 265.5,322 L 265.5,336L 265.5,350L 265.5,364Q 265.5,371 270,371Q 270,371 279,371L 288,371L 297,371L 306,371L 315,371L 324,371L 333,371L 342,371L 351,371L 360,371L 369,371L 378,371L 387,371L 396,371L 405,371L 414,371L 423,371L 432,371L 441,371L 450,371L 459,371L 463.5,371L 477,392" />
<path stroke-width="10" d="M 400.5,322 L 400.5,336Q 400.5,343 405,343Q 405,343 414,343L 423,343L 432,343L 441,343L 450,343L 459,343Q 463.5,343 463.5,350L 463.5,364L 463.5,371" id="m_v1" fill="none" stroke="orange" />
<path stroke-width="2" stroke="green" d="M 310.5,378 L 310.5,392L 310.5,406L 310.5,420" fill="none" />
<path stroke="green" stroke-width="2" d="M 445.5,378 L 445.5,392L 445.5,406L 445.5,420" fill="none" />
<text font-family="Menlo" font-size="14" y="7" x="9" dominant-baseline="middle" fill="black" text-anchor="start" xml:space="preserve">(tx, rx) = channel();</text>
<text x="477" font-family="Menlo" fill="black" xml:space="preserve" dominant-baseline="middle" y="49" text-anchor="start" font-size="14">rx</text>
<text fill="black" font-family="Menlo" xml:space="preserve" y="91" font-size="14" text-anchor="start" x="18" dominant-baseline="middle">t1 = tx.clone() </text>
<text x="324" dominant-baseline="middle" fill="black" y="133" font-family="Menlo" font-size="14" text-anchor="start" xml:space="preserve">t1 </text>
<text y="175" text-anchor="start" font-family="Menlo" font-size="14" xml:space="preserve" dominant-baseline="middle" x="63" fill="black">tx </text>
<text xml:space="preserve" dominant-baseline="middle" font-size="14" text-anchor="start" fill="black" x="189" y="217" font-family="Menlo">tx </text>
<text font-family="Menlo" dominant-baseline="middle" font-size="14" xml:space="preserve" text-anchor="start" fill="black" y="245" x="207">v = Vec </text>
<text dominant-baseline="middle" x="342" y="245" font-size="14" font-family="Menlo" fill="black" text-anchor="start" xml:space="preserve">v = Vec </text>
<text text-anchor="start" xml:space="preserve" font-size="14" fill="black" font-family="Menlo" x="207" y="259" dominant-baseline="middle">v.push('1')</text>
<text dominant-baseline="middle" font-family="Menlo" xml:space="preserve" x="342" font-size="14" fill="black" text-anchor="start" y="259">v.push('a')</text>
<text text-anchor="start" xml:space="preserve" font-family="Menlo" fill="black" y="273" x="207" dominant-baseline="middle" font-size="14">v.push('2')</text>
<text font-family="Menlo" font-size="14" y="273" xml:space="preserve" text-anchor="start" dominant-baseline="middle" fill="black" x="342">v.push('b')</text>
<text xml:space="preserve" font-size="14" dominant-baseline="middle" x="207" font-family="Menlo" y="287" fill="black" text-anchor="start">v.push('3')</text>
<text dominant-baseline="middle" x="342" fill="black" font-size="14" text-anchor="start" font-family="Menlo" xml:space="preserve" y="287">v.push('c')</text>
<text font-size="14" font-family="Menlo" fill="black" x="189" xml:space="preserve" y="315" dominant-baseline="middle" text-anchor="start">tx.send(v) </text>
<text dominant-baseline="middle" text-anchor="start" xml:space="preserve" x="324" font-size="14" font-family="Menlo" y="315" fill="black">t1.send(v) </text>
<text fill="black" font-size="14" y="357" dominant-baseline="middle" xml:space="preserve" font-family="Menlo" text-anchor="start" x="441">| </text>
<text x="459" xml:space="preserve" fill="black" font-family="Menlo" text-anchor="start" y="399" dominant-baseline="middle" font-size="14">Ok(v) = rx.recv()</text>
<text dominant-baseline="middle" font-family="Menlo" font-size="14" y="413" text-anchor="start" xml:space="preserve" fill="black" x="459">println!("A1 received {:?}", v)</text>
</svg>
<ul>
<li class="fragment"><code class="sourceCode rust">rx</code>, <code class="sourceCode rust">t<span class="dv">1</span></code>, <code class="sourceCode rust">tx</code>, and each <code class="sourceCode rust">v</code> are all <em>moved</em> from one thread to another</li>
<li class="fragment"><code class="sourceCode rust">t<span class="dv">1</span></code> <em>cloned</em> from <code class="sourceCode rust">tx</code>; (if <code class="sourceCode rust">tx</code> moved to middle subthread, would not be available for left one)</li>
</ul>
</section>
<section id="not-everything-can-be-sent" class="slide level2">
<h2>Not everything can be sent</h2>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> demo_sendable() <span class="op">{</span>
<span class="kw">let</span> (tx, rx) = channel();
<span class="kw">let</span> r1 = Rc::new(<span class="pp">vec!</span><span class="op">[</span><span class="ch">'a'</span>, <span class="ch">'b'</span>, <span class="ch">'c'</span>, <span class="ch">'1'</span>, <span class="ch">'2'</span>, <span class="ch">'3'</span><span class="op">]</span>);
tx.send(r1.clone()).unwrap();
<span class="kw">let</span> _r2 = rx.recv().unwrap();
<span class="pp">println!</span>(<span class="st">"r1: {:?}"</span>, r1);
<span class="op">}</span></code></pre></div>
<div class="fragment">
<p>(above is fine)</p>
<ul>
<li class="fragment">FYI: <code class="sourceCode rust">Rc<T></code> is a reference-counted pointer to a heap-allocated <code class="sourceCode rust">T</code></li>
<li class="fragment">(Like <code class="sourceCode rust"><span class="dt">Box</span><T></code>, but with dynamically-tracked shared ownership rather than statically-tracked sole ownership)</li>
</ul>
</div>
</section>
<section id="not-everything-can-be-sent-really" class="slide level2">
<h2>Not everything can be sent (really)</h2>
<div class="sourceCode"><pre class="sourceCode rust compile_error"><code class="sourceCode rust"><span class="kw">fn</span> demo_unsendable() <span class="op">{</span>
<span class="kw">let</span> (tx, rx) = channel();
<span class="kw">let</span> r1 = Rc::new(<span class="pp">vec!</span><span class="op">[</span><span class="ch">'a'</span>, <span class="ch">'b'</span>, <span class="ch">'c'</span>, <span class="ch">'1'</span>, <span class="ch">'2'</span>, <span class="ch">'3'</span><span class="op">]</span>);
thread::spawn(<span class="kw">move</span> || <span class="op">{</span> <span class="kw">let</span> r2 = rx.recv().unwrap();
<span class="pp">println!</span>(<span class="st">"received: {:?}"</span>, r2); <span class="op">}</span>);
tx.send(r1.clone()).unwrap();
<span class="pp">println!</span>(<span class="st">"r1: {:?}"</span>, r1);
<span class="op">}</span></code></pre></div>
<pre class="terminal"><code>error[E0277]: the trait bound `Rc<Vec<char>>: Send` is not satisfied
--> src/a00.rs:405:5
|
405 | thread::spawn(move || {
| ^^^^^^^^^^^^^ `Rc<Vec<char>>` cannot be sent between threads safely
|
= help: the trait `Send` is not implemented for `Rc<Vec<char>>`
= note: required because of the requirements on the impl of `Send`
for `Receiver<Rc<Vec<char>>>`
= note: required because it appears within the type
`[closure@src/a00.rs:405:19: 410:6 Receiver<Rc<Vec<char>>>]`
= note: required by `std::thread::spawn`</code></pre>
<div class="fragment">
<p>Will revisit these constraints a bit more later</p>
<p>Digression: talk about sharing for a moment</p>
</div>
</section>
<section id="move-semantics-alone-insufficient" class="slide level2">
<h2>Move semantics alone: insufficient</h2>
<h3 id="we-are-computer-scientists">We are computer scientists</h3>
<div class="fragment">
<blockquote>
<p>We can solve any problem by introducing an extra level of indirection</p>
</blockquote>
<p>-- David J. Wheeler</p>
</div>
<div class="fragment">
<p>We need references; allows distinguishing:</p>
<ul>
<li class="fragment">ownership transfer, versus</li>
<li class="fragment">lending access temporarily</li>
</ul>
</div>
</section>
<section id="importance-of-ownership" class="slide level2">
<h2>Importance of Ownership</h2>
<table>
<thead>
<tr class="header">
<th style="text-align: left;">Ownership enables:</th>
<th style="text-align: left;">which removes:</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">RAII-style destructors</td>
<td style="text-align: left;">source of memory leaks (or fd leaks, etc)</td>
</tr>
<tr class="even">
<td style="text-align: left;">no dangling pointers</td>
<td style="text-align: left;">many resource management bugs</td>
</tr>
<tr class="odd">
<td style="text-align: left;">no data races</td>
<td style="text-align: left;">many multithreading heisenbugs</td>
</tr>
</tbody>
</table>
<div class="fragment">
<blockquote>
<p>Do I need to take ownership here, accepting the associated resource management responsibility? Would temporary access suffice?</p>
</blockquote>
</div>
<div class="fragment">
<p>Good developers ask this already!</p>
<p>Rust forces function signatures to encode the answers</p>
<p>(and they are checked by the compiler)</p>
</div>
</section>
<section id="sharing-data" class="slide level2">
<h2>Sharing Data</h2>
<p>Pointers: Perhaps the Pandora's Box of Computer Science</p>
<ul>
<li class="fragment"><code class="sourceCode rust">&<span class="kw">mut</span> T</code>, <code class="sourceCode rust">&T</code></li>
<li class="fragment">(plus library reference types: <code>Box<T></code>, <code>Cow<T></code>, <code>Rc<T></code>, ...)</li>
<li class="fragment"><code class="sourceCode rust">T</code> above ranges over both so-called "sized" and "unsized" types</li>
<li class="fragment">Sized: <code class="sourceCode rust">&<span class="dt">char</span></code>, <code class="sourceCode rust">&<span class="kw">mut</span> <span class="dt">Vec</span><<span class="dt">u8</span>></code>, <code class="sourceCode rust">&<span class="op">[</span><span class="dt">i32</span>; <span class="dv">16</span><span class="op">]</span></code></li>
<li class="fragment">Unsized: <code class="sourceCode rust">&<span class="dt">str</span></code>, <code class="sourceCode rust">&<span class="kw">mut</span> <span class="op">[</span><span class="dt">u8</span><span class="op">]</span></code>, <code class="sourceCode rust">&<span class="bu">Fn</span>() -> <span class="dt">i32</span></code></li>
</ul>
</section>
<section id="menagerie-t-boxt-rct-etc" class="slide level2">
<h2>Menagerie: <code class="sourceCode rust">&T</code>, <code class="sourceCode rust"><span class="dt">Box</span><T></code>, <code class="sourceCode rust">Rc<T></code> etc</h2>
<ul>
<li class="fragment"><code class="sourceCode rust">T</code>: owned instance of <code class="sourceCode rust">T</code>, stored inline (e.g. in stack frame or record)</li>
<li class="fragment"><code class="sourceCode rust"><span class="dt">Box</span><T></code>: owned instance of <code class="sourceCode rust">T</code>, stored on heap (so <code class="sourceCode rust"><span class="dt">Box</span><T></code> itself is just a pointer)</li>
<li class="fragment"><code class="sourceCode rust">&T</code>: pointer to <code class="sourceCode rust">T</code>, but not owned. Extent is limited to some static scope (possibly a scope known only to function's caller).</li>
<li class="fragment"><code class="sourceCode rust">Rc<T></code>: ref-counted pointer to <code>T</code>; <em>shared ownership</em>. (At end of scope for our <code class="sourceCode rust">Rc<T></code>, we <em>might</em> be responsible for resource cleanup.)</li>
</ul>
</section>
<section id="why-multiple--reference-types" class="slide level2 center">
<h2>Why multiple <code>&</code>-reference types?</h2>
<ul>
<li><p>Distinguish <em>exclusive</em> access from <em>shared</em> access</p></li>
<li><p>Enables <strong>safe</strong>, <strong>parallel</strong> API's</p></li>
</ul>
<div class="fragment">
<table>
<tbody>
<tr class="odd">
<td style="text-align: left;">Ownership</td>
<td style="text-align: left;"><code class="sourceCode rust">T</code></td>
<td></td>
</tr>
<tr class="even">
<td style="text-align: left;">Exclusive access</td>
<td style="text-align: left;"><code class="sourceCode rust">&<span class="kw">mut</span> T</code></td>
<td>("mutable")</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Shared access</td>
<td style="text-align: left;"><code class="sourceCode rust">&T</code></td>
<td>("read-only")</td>
</tr>
</tbody>
</table>
</div>
<div class="fragment">
<p>A <code class="sourceCode rust">&T</code> provides limited access; cannot call methods that require ownership or exclusive access to <code class="sourceCode rust">T</code></p>
</div>
<div class="fragment">
<p>A <code class="sourceCode rust">&<span class="kw">mut</span> T</code> provides <em>temporary</em> exclusive access; even the original owner cannot mutate the object while you have the reference</p>
</div>
<div class="fragment">
<p>But cannot claim ownership of <code class="sourceCode rust">T</code> yourself via <code class="sourceCode rust">&<span class="kw">mut</span> T</code>, unless you <em>swap</em> in another <code class="sourceCode rust">T</code> to replace one you take (Rust coding pattern)</p>
</div>
</section>
<section id="references-and-slices" class="slide level2">
<h2>References and Slices</h2>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> demo_references() <span class="op">{</span>
<span class="kw">let</span> v = <span class="pp">vec!</span><span class="op">[</span><span class="ch">'a'</span>, <span class="ch">'b'</span>, <span class="ch">'c'</span>, <span class="ch">'1'</span>, <span class="ch">'2'</span>, <span class="ch">'3'</span><span class="op">]</span>;
<span class="kw">let</span> ref1 = &v<span class="op">[</span><span class="dv">0.</span>.<span class="dv">3</span><span class="op">]</span>;
<span class="kw">let</span> ref2 = &v<span class="op">[</span><span class="dv">3.</span>.<span class="dv">6</span><span class="op">]</span>;
<span class="kw">let</span> f1 = |i| <span class="pp">println!</span>(<span class="st">"ref1[{}]: {:?}"</span>, i, ref1<span class="op">[</span>i<span class="op">]</span>);
<span class="kw">let</span> f2 = |i| <span class="pp">println!</span>(<span class="st">"ref2[{}]: {:?}"</span>, i, ref2<span class="op">[</span>i<span class="op">]</span>);
f1(<span class="dv">1</span>);
f2(<span class="dv">2</span>);
<span class="op">}</span></code></pre></div>
<svg height="252" width="675" baseProfile="full" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 675 252" version="1.1">
<path stroke-width="2" fill="none" d="M 598.5,49L 612,49L 621,49L 630,49L 639,49L 648,49L 657,49L 666,49L 670.5,49L 670.5,70L 670.5,84L 670.5,98L 670.5,112L 670.5,126L 670.5,140L 670.5,154L 670.5,168L 670.5,182L 670.5,196L 670.5,210L 670.5,224L 670.5,238L 670.5,245L 657,245L 648,245L 639,245L 630,245L 621,245L 612,245L 603,245L 598.5,245L 598.5,224L 598.5,210L 598.5,196L 598.5,182L 598.5,168L 598.5,154L 598.5,140L 598.5,126L 598.5,112L 598.5,98L 598.5,84L 598.5,70L 598.5,56 Z" stroke="green" />
<path id="v" stroke="black" stroke-width="2" d="M 54,7 L 63,7L 72,7L 81,7L 90,7L 99,7L 108,7L 117,7L 126,7L 135,7L 144,7L 153,7L 162,7L 171,7L 180,7L 189,7L 198,7L 207,7L 216,7L 225,7L 234,7L 243,7L 252,7L 261,7L 270,7L 279,7L 288,7L 297,7L 306,7L 315,7L 324,7L 333,7L 342,7L 351,7L 360,7L 369,7L 378,7L 387,7L 396,7L 405,7L 414,7L 423,7L 432,7L 441,7L 450,7L 459,7L 468,7L 477,7L 486,7L 495,7L 504,7L 513,7L 522,7L 531,7L 540,7L 549,7L 558,7L 567,7L 576,7L 585,7L 594,7L 603,7Q 607.5,7 607.5,14L 607.5,28L 607.5,35 l 0,5 m -3,-5 l 3, 5 l 3,-5 m -3,0" fill="none" />
<path fill="none" stroke="green" d="M 585,133 L 576,133Q 571.5,133 571.5,126L 571.5,112L 571.5,98L 571.5,84L 571.5,70Q 571.5,63 576,63Q 576,63 585,63" stroke-width="2" />
<path id="ref1" stroke-dasharray="5,2" stroke="red" d="M 81,91 L 90,91L 99,91L 108,91L 117,91L 126,91L 135,91L 144,91L 153,91L 162,91L 171,91L 180,91L 189,91L 198,91L 207,91L 216,91L 225,91L 234,91L 243,91L 252,91L 261,91L 270,91L 279,91L 288,91L 297,91L 306,91L 315,91L 324,91L 333,91L 342,91L 351,91L 360,91L 369,91L 378,91L 387,91L 396,91L 405,91L 414,91L 423,91L 432,91L 441,91L 450,91L 459,91L 468,91L 477,91L 486,91L 495,91L 504,91L 513,91L 522,91L 531,91L 540,91L 549,91L 558,91L 562.5,91 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" fill="none" stroke-width="2" />
<path d="M 585,231 L 576,231Q 571.5,231 571.5,224L 571.5,210L 571.5,196L 571.5,182L 571.5,168L 571.5,154Q 571.5,147 576,147Q 576,147 585,147" stroke="green" stroke-width="2" fill="none" />
<path fill="none" stroke="red" stroke-width="2" id="ref2" stroke-dasharray="5,2" d="M 81,175 L 90,175L 99,175L 108,175L 117,175L 126,175L 135,175L 144,175L 153,175L 162,175L 171,175L 180,175L 189,175L 198,175L 207,175L 216,175L 225,175L 234,175L 243,175L 252,175L 261,175L 270,175L 279,175L 288,175L 297,175L 306,175L 315,175L 324,175L 333,175L 342,175L 351,175L 360,175L 369,175L 378,175L 387,175L 396,175L 405,175L 414,175L 423,175L 432,175L 441,175L 450,175L 459,175L 468,175L 477,175L 486,175L 495,175L 504,175L 513,175L 522,175L 531,175L 540,175L 549,175L 558,175L 562.5,175 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" />
<text y="7" font-size="14" x="36" xml:space="preserve" text-anchor="start" font-family="Menlo" fill="black" dominant-baseline="middle">v </text>
<text y="21" xml:space="preserve" text-anchor="start" font-size="14" fill="black" font-family="Menlo" dominant-baseline="middle" x="612">(r)</text>
<text font-size="14" x="531" xml:space="preserve" font-family="Menlo" dominant-baseline="middle" y="77" fill="black" text-anchor="start">(r) </text>
<text y="77" text-anchor="start" xml:space="preserve" font-family="Menlo" x="612" fill="black" dominant-baseline="middle" font-size="14">'a' </text>
<text text-anchor="start" xml:space="preserve" x="36" fill="black" font-size="14" font-family="Menlo" y="91" dominant-baseline="middle">ref1 </text>
<text font-family="Menlo" font-size="14" fill="black" x="612" dominant-baseline="middle" text-anchor="start" xml:space="preserve" y="105">'b' </text>
<text font-family="Menlo" dominant-baseline="middle" y="133" fill="black" text-anchor="start" xml:space="preserve" x="612" font-size="14">'c' </text>
<text text-anchor="start" xml:space="preserve" fill="black" dominant-baseline="middle" y="161" font-family="Menlo" font-size="14" x="531">(r) </text>
<text dominant-baseline="middle" font-family="Menlo" y="161" text-anchor="start" xml:space="preserve" x="612" font-size="14" fill="black">'1' </text>
<text y="175" font-size="14" text-anchor="start" dominant-baseline="middle" xml:space="preserve" x="36" fill="black" font-family="Menlo">ref2 </text>
<text fill="black" y="189" x="612" dominant-baseline="middle" font-family="Menlo" xml:space="preserve" text-anchor="start" font-size="14">'2' </text>
<text dominant-baseline="middle" fill="black" font-size="14" x="612" font-family="Menlo" xml:space="preserve" y="217" text-anchor="start">'3' </text>
</svg>
</section>
<section id="rct-shared-ownership" class="slide level2">
<h2><code class="sourceCode rust">Rc<T></code> (shared ownership)</h2>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> demo_refcounts() <span class="op">{</span>
<span class="kw">let</span> r1 = Rc::new(<span class="pp">vec!</span><span class="op">[</span><span class="ch">'a'</span>, <span class="ch">'b'</span>, <span class="ch">'c'</span>, <span class="ch">'1'</span>, <span class="ch">'2'</span>, <span class="ch">'3'</span><span class="op">]</span>);
<span class="kw">let</span> r2 = r1.clone();
<span class="kw">let</span> f1 = |i| <span class="pp">println!</span>(<span class="st">"v[{}]: {:?}"</span>, i, r1<span class="op">[</span>i<span class="op">]</span>);
<span class="kw">let</span> f2 = |i| <span class="pp">println!</span>(<span class="st">"v[{}]: {:?}"</span>, i, r2<span class="op">[</span>i<span class="op">]</span>);
f1(<span class="dv">1</span>);
f2(<span class="dv">2</span>);
<span class="op">}</span></code></pre></div>
<svg width="675" baseProfile="full" version="1.1" viewBox="0 0 675 280" xmlns="http://www.w3.org/2000/svg" height="280">
<path stroke="green" fill="none" d="M 598.5,77L 612,77L 621,77L 630,77L 639,77L 648,77L 657,77L 666,77L 670.5,77L 670.5,98L 670.5,112L 670.5,126L 670.5,140L 670.5,154L 670.5,168L 670.5,182L 670.5,196L 670.5,210L 670.5,224L 670.5,238L 670.5,252L 670.5,266L 670.5,273L 657,273L 648,273L 639,273L 630,273L 621,273L 612,273L 603,273L 598.5,273L 598.5,252L 598.5,238L 598.5,224L 598.5,210L 598.5,196L 598.5,182L 598.5,168L 598.5,154L 598.5,140L 598.5,126L 598.5,112L 598.5,98L 598.5,84 Z" stroke-width="2" />
<path d="M 499.5,42 L 499.5,56L 499.5,70L 499.5,77L 486,77L 477,77L 468,77L 459,77L 450,77L 441,77L 432,77L 423,77L 414,77L 405,77L 396,77L 387,77L 382.5,77L 382.5,56L 382.5,42L 382.5,28L 382.5,14L 382.5,7L 396,7L 405,7L 414,7L 423,7L 432,7L 441,7L 450,7L 459,7L 468,7L 477,7L 486,7L 495,7L 499.5,7L 499.5,28" stroke-width="2" stroke="green" fill="none" />
<path stroke-width="2" id="r1" d="M 63,21 L 72,21L 81,21L 90,21L 99,21L 108,21L 117,21L 126,21L 135,21L 144,21L 153,21L 162,21L 171,21L 180,21L 189,21L 198,21L 207,21L 216,21L 225,21L 234,21L 243,21L 252,21L 261,21L 270,21L 279,21L 288,21L 297,21L 306,21L 315,21L 324,21L 333,21L 342,21L 351,21L 360,21L 369,21L 373.5,21 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" stroke="black" fill="none" />
<path stroke-width="2" fill="none" stroke-dasharray="5,2" stroke="green" d="M 463.5,28 L 463.5,42L 463.5,56L 463.5,70" />
<path stroke-width="2" stroke="green" fill="none" d="M 477,35 L 486,35L 495,35L 504,35L 513,35L 522,35L 531,35L 540,35L 549,35L 558,35L 567,35L 576,35L 585,35L 594,35L 603,35Q 607.5,35 607.5,42L 607.5,56L 607.5,63 l 0,5 m -3,-5 l 3, 5 l 3,-5 m -3,0" />
<path stroke="black" fill="none" d="M 63,63 L 72,63L 81,63L 90,63L 99,63L 108,63L 117,63L 126,63L 135,63L 144,63L 153,63L 162,63L 171,63L 180,63L 189,63L 198,63L 207,63L 216,63L 225,63L 234,63L 243,63L 252,63L 261,63L 270,63L 279,63L 288,63L 297,63L 306,63L 315,63L 324,63L 333,63L 342,63L 351,63L 360,63L 369,63L 373.5,63 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" id="r2" stroke-width="2" />
<text font-size="14" text-anchor="start" font-family="Menlo" xml:space="preserve" dominant-baseline="middle" fill="black" x="36" y="21">r1 </text>
<text x="396" y="21" font-family="Menlo" xml:space="preserve" text-anchor="start" dominant-baseline="middle" fill="black" font-size="14">count: 2 </text>
<text x="396" font-size="14" fill="black" xml:space="preserve" y="35" text-anchor="start" font-family="Menlo" dominant-baseline="middle">Vec buf</text>
<text fill="black" dominant-baseline="middle" xml:space="preserve" font-family="Menlo" x="432" text-anchor="start" font-size="14" y="49">len</text>
<text fill="black" font-family="Menlo" y="49" dominant-baseline="middle" x="477" text-anchor="start" xml:space="preserve" font-size="14">6 </text>
<text dominant-baseline="middle" font-size="14" font-family="Menlo" text-anchor="start" fill="black" y="63" x="36" xml:space="preserve">r2 </text>
<text font-family="Menlo" fill="black" y="63" font-size="14" dominant-baseline="middle" x="432" xml:space="preserve" text-anchor="start">cap</text>
<text xml:space="preserve" text-anchor="start" x="477" font-size="14" font-family="Menlo" fill="black" y="63" dominant-baseline="middle">8 </text>
<text dominant-baseline="middle" x="612" font-size="14" xml:space="preserve" text-anchor="start" fill="black" y="105" font-family="Menlo">'a' </text>
<text text-anchor="start" dominant-baseline="middle" xml:space="preserve" y="133" fill="black" font-size="14" x="612" font-family="Menlo">'b' </text>
<text font-size="14" text-anchor="start" dominant-baseline="middle" y="161" fill="black" xml:space="preserve" x="612" font-family="Menlo">'c' </text>
<text fill="black" y="189" xml:space="preserve" font-family="Menlo" font-size="14" x="612" dominant-baseline="middle" text-anchor="start">'1' </text>
<text x="612" fill="black" text-anchor="start" xml:space="preserve" y="217" font-family="Menlo" font-size="14" dominant-baseline="middle">'2' </text>
<text font-family="Menlo" y="245" x="612" dominant-baseline="middle" xml:space="preserve" fill="black" font-size="14" text-anchor="start">'3' </text>
</svg>
</section>
<section id="is-mut-only-way-to-encode-mutation-via-reference" class="slide level2">
<h2>Is <code class="sourceCode rust">&<span class="kw">mut</span></code> only way to encode mutation via reference?</h2>
<div class="fragment">
<p>(No!)</p>
</div>
</section>
<section id="interior-mutability-cell-refcell" class="slide level2">
<h2>Interior Mutability: <code class="sourceCode rust">Cell</code> + <code class="sourceCode rust">RefCell</code></h2>
<ul>
<li class="fragment">Both types have mutator methods that take <code class="sourceCode rust">&<span class="kw">self</span></code> (<em>not</em> <code class="sourceCode rust">&<span class="kw">mut</span> <span class="kw">self</span></code>)</li>
<li class="fragment"><code class="sourceCode rust">Cell<T></code>: <em>move</em> values in+out (via swapping, or other methods that ensure cell remains sound, e.g. get/set if <code class="sourceCode rust">T: <span class="bu">Copy</span></code>)</li>
<li class="fragment"><code class="sourceCode rust">RefCell<T></code> provides refs to the <code class="sourceCode rust">T</code> it holds, but <em>dynamically</em> <em>enforces</em> the rules.</li>
<li class="fragment"><code class="sourceCode rust">RefCell::borrow()</code> returns read-only <code class="sourceCode rust">Ref</code> (many-readers), and <code class="sourceCode rust"><span class="pp">panic!</span></code>'s on outstanding mut-borrow (∴ no previous writers)</li>
<li class="fragment"><code class="sourceCode rust">RefCell::borrow_mut()</code> returns read/write <code class="sourceCode rust">RefMut</code> (unique), and <code class="sourceCode rust"><span class="pp">panic!</span></code>'s on any outstanding borrow (∴ no previous readers or writers).</li>
</ul>
</section>
<section id="sharing-resources-rayon" class="slide level2">
<h2>Sharing resources (<code>rayon</code>)</h2>
<!--
```rust
use rayon;
use std::sync::mpsc::*;
#[test]
fn demo_send2() {
```
-->
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">let</span> temp_data = <span class="pp">vec!</span><span class="op">[</span><span class="ch">'a'</span>, <span class="ch">'b'</span>, <span class="ch">'c'</span>, <span class="ch">'1'</span>, <span class="ch">'2'</span>, <span class="ch">'3'</span><span class="op">]</span>;
rayon::scope(|s| <span class="op">{</span>
<span class="kw">let</span> (tx, rx) = channel(); <span class="co">// create transmitter/receiver pair</span>
s.spawn(<span class="kw">move</span> |_s| <span class="op">{</span> <span class="co">// spawn A1, taking ownership of `rx`</span>
<span class="kw">match</span> rx.recv() <span class="op">{</span>
<span class="cn">Ok</span>(v) => <span class="pp">println!</span>(<span class="st">"A1 received {:?}"</span>, v),
<span class="cn">Err</span>(err) => <span class="pp">println!</span>(<span class="st">"A1 receive failure {:?}"</span>, err),
<span class="op">}</span>
<span class="op">}</span>);
<span class="kw">let</span> data: &<span class="op">[</span><span class="dt">char</span><span class="op">]</span> = &temp_data; <span class="co">// (N.B. assigned type is *not* `&Vec`)</span>
<span class="kw">let</span> t1 = tx.clone();
<span class="co">// spawn A2, taking ownership of `t1` (and a copy of `data`)</span>
s.spawn(<span class="kw">move</span> |_s| t1.send(&data<span class="op">[</span><span class="dv">0.</span>.<span class="dv">4</span><span class="op">]</span>).expect(<span class="st">"A2 send failure"</span>));
<span class="co">// spawn A3, taking ownership of `tx` (and a copy of `data`)</span>
s.spawn(<span class="kw">move</span> |_s| tx.send(&data<span class="op">[</span><span class="dv">2.</span>.<span class="dv">6</span><span class="op">]</span>).expect(<span class="st">"A3 send failure"</span>));
<span class="op">}</span>); <span class="co">// (`rayon::scope` waits here until all `s.spawn`'ed threads finish)</span></code></pre></div>
<!--
```rust
}
```
-->
</section>
<section id="sharing-resources-between-threads" class="slide level2">
<h2>Sharing resources between threads</h2>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 765 490" baseProfile="full" version="1.1" height="490" width="765">
<path stroke-width="2" d="M 616.5,63L 630,63L 639,63L 648,63L 657,63L 666,63L 675,63L 679.5,63L 679.5,84L 679.5,98L 679.5,112L 679.5,126L 679.5,140L 679.5,154L 679.5,168L 679.5,182L 679.5,196L 679.5,210L 679.5,224L 679.5,238L 679.5,252L 679.5,259L 666,259L 657,259L 648,259L 639,259L 630,259L 621,259L 616.5,259L 616.5,238L 616.5,224L 616.5,210L 616.5,196L 616.5,182L 616.5,168L 616.5,154L 616.5,140L 616.5,126L 616.5,112L 616.5,98L 616.5,84L 616.5,70 Z" stroke="green" fill="none" />
<path d="M 4.5,0 L 4.5,14L 4.5,28L 4.5,42L 4.5,56L 4.5,70L 4.5,77L 4.5,98L 4.5,112L 4.5,126L 4.5,140L 4.5,154L 4.5,168L 4.5,175L 4.5,196L 4.5,210L 4.5,224L 4.5,238L 4.5,252L 4.5,259L 18,259L 27,259L 36,259L 45,259L 54,259L 63,259L 72,259L 81,259L 90,259L 99,259L 108,259L 117,259L 126,259L 135,259L 144,259L 153,259L 162,259L 171,259Q 175.5,259 175.5,266L 175.5,280L 175.5,294L 175.5,308L 175.5,322L 175.5,336L 175.5,350L 175.5,364L 175.5,378L 175.5,392L 175.5,406L 175.5,420L 175.5,434L 175.5,448L 175.5,462L 175.5,469L 162,469L 153,469L 144,469L 135,469L 126,469L 117,469L 108,469L 99,469L 90,469L 81,469L 72,469L 63,469L 54,469L 45,469L 36,469L 27,469L 18,469L 9,469L 4.5,469L 4.5,490" fill="none" stroke="green" stroke-width="2" />
<path stroke-width="2" id="temp_data" stroke="black" d="M 108,7 L 117,7L 126,7L 135,7L 144,7L 153,7L 162,7L 171,7L 180,7L 189,7L 198,7L 207,7L 216,7L 225,7L 234,7L 243,7L 252,7L 261,7L 270,7L 279,7L 288,7L 297,7L 306,7L 315,7L 324,7L 333,7L 342,7L 351,7L 360,7L 369,7L 378,7L 387,7L 396,7L 405,7L 414,7L 423,7L 432,7L 441,7L 450,7L 459,7L 468,7L 477,7L 486,7L 495,7L 504,7L 513,7L 522,7L 531,7L 540,7L 549,7L 558,7L 567,7L 576,7L 585,7L 594,7L 603,7L 612,7L 621,7Q 625.5,7 625.5,14L 625.5,28L 625.5,42L 625.5,49 l 0,5 m -3,-5 l 3, 5 l 3,-5 m -3,0" fill="none" />
<path id="txa" fill="none" stroke-dasharray="5,2" stroke-width="2" d="M 22.5,42 L 22.5,56L 22.5,70L 22.5,84Q 22.5,91 27,91Q 27,91 36,91L 45,91L 54,91L 63,91Q 67.5,91 67.5,98L 67.5,112L 67.5,126" stroke="blue" />
<path d="M 67.5,42 Q 67.5,49 72,49Q 72,49 81,49L 90,49L 99,49L 108,49L 117,49L 126,49L 135,49L 144,49L 153,49L 162,49L 171,49L 180,49L 189,49L 198,49L 207,49L 216,49L 225,49L 234,49L 243,49L 252,49L 261,49L 270,49L 279,49L 288,49L 297,49L 306,49L 315,49L 324,49L 333,49L 342,49L 351,49L 360,49L 369,49L 378,49L 387,49L 396,49L 405,49L 414,49L 423,49L 432,49L 441,49L 450,49L 459,49L 468,49L 477,49Q 481.5,49 481.5,56L 481.5,70" stroke="orange" stroke-width="10" fill="none" id="move_rx" />
<path stroke="green" stroke-width="2" d="M 4.5,77L 18,77" fill="none" />
<path stroke="green" stroke-width="2" fill="none" d="M 27,77 L 36,77L 45,77L 54,77L 63,77L 72,77L 81,77L 90,77L 99,77L 108,77L 117,77L 126,77L 135,77L 144,77L 153,77L 162,77L 171,77L 180,77L 189,77L 198,77L 207,77L 216,77L 225,77L 234,77L 243,77L 252,77L 261,77L 270,77L 279,77L 288,77L 297,77L 306,77L 315,77L 324,77L 333,77L 342,77L 351,77L 360,77L 369,77L 378,77L 387,77L 396,77L 405,77L 414,77L 423,77L 432,77L 441,77Q 445.5,77 445.5,84L 445.5,98L 445.5,112L 445.5,126L 445.5,140L 445.5,154L 445.5,168" />
<path id="d2" stroke-dasharray="5,2" stroke-width="2" stroke="black" d="M 279,259 L 288,259L 297,259L 306,259L 315,259L 324,259L 333,259L 342,259L 351,259L 360,259L 369,259L 378,259L 387,259L 396,259L 405,259L 414,259L 423,259L 432,259L 441,259L 450,259L 459,259L 468,259L 477,259L 486,259L 495,259L 504,259L 513,259Q 517.5,259 522,252L 531,238L 540,224L 549,210L 558,196L 567,182L 576,168L 580.5,161L 580.5,140L 580.5,126L 580.5,119L 580.5,98L 580.5,84Q 580.5,77 585,77Q 585,77 594,77L 603,77L 607.5,77 l 3,0 m -3,-3 l 3,3 l -3,3 m 0,-3" fill="none" />
<path fill="none" d="M 720,77 L 729,77Q 733.5,77 733.5,84L 733.5,98L 733.5,112L 733.5,126L 733.5,140L 733.5,154L 733.5,168Q 733.5,175 729,175Q 729,175 720,175" stroke="green" stroke-width="2" />
<path stroke="blue" stroke-dasharray="5,2" d="M 481.5,84 L 481.5,98L 481.5,112L 481.5,126L 481.5,140L 481.5,154L 481.5,168" stroke-width="2" id="rxa" fill="none" />
<path stroke="black" fill="none" d="M 742.5,105 l -3,0 m 3,-3 l -3,3 l 3,3 m 0,-3L 756,105Q 760.5,105 760.5,112L 760.5,126L 760.5,140L 760.5,154L 760.5,168L 760.5,182L 760.5,196L 760.5,210L 760.5,224L 760.5,238L 760.5,252L 760.5,266L 760.5,280L 760.5,294L 760.5,308L 760.5,322L 760.5,336Q 760.5,343 756,343Q 756,343 747,343L 738,343L 729,343L 720,343L 711,343L 702,343L 693,343L 684,343L 675,343L 666,343L 657,343L 648,343L 639,343L 630,343L 621,343L 612,343L 603,343L 594,343L 585,343L 576,343L 567,343L 558,343L 549,343L 540,343L 531,343L 522,343L 513,343L 504,343L 495,343L 486,343L 477,343L 468,343L 459,343L 450,343L 441,343L 432,343L 423,343L 414,343L 405,343L 396,343L 387,343L 378,343" id="s1" stroke-dasharray="5,2" stroke-width="2" />
<path d="M 580.5,119L 567,140L 558,154L 549,168Q 544.5,175 540,175Q 540,175 531,175L 522,175L 513,175L 504,175L 495,175L 486,175L 477,175L 468,175L 459,175L 450,175L 441,175L 432,175L 423,175L 414,175" id="d1" stroke="black" stroke-dasharray="5,2" stroke-width="2" fill="none" />
<path stroke="green" d="M 693,133 L 702,133Q 706.5,133 706.5,140L 706.5,154L 706.5,168L 706.5,182L 706.5,196L 706.5,210L 706.5,224Q 706.5,231 702,231Q 702,231 693,231" stroke-width="2" fill="none" />
<path stroke-width="10" stroke="orange" fill="none" id="m_t1" d="M 31.5,140 Q 31.5,147 36,147Q 36,147 45,147L 54,147L 63,147L 72,147L 81,147L 90,147L 99,147L 108,147L 117,147L 126,147L 135,147L 144,147L 153,147L 162,147L 171,147L 180,147L 189,147L 198,147L 207,147L 216,147L 225,147L 234,147L 243,147L 252,147L 261,147L 270,147L 279,147L 288,147L 297,147L 306,147L 315,147L 324,147Q 328.5,147 328.5,154L 328.5,168" />
<path stroke-dasharray="5,2" stroke-width="2" stroke="blue" fill="none" d="M 67.5,154 L 67.5,168L 67.5,182L 67.5,196L 67.5,210" id="txb" />
<path fill="none" stroke-width="2" stroke="green" d="M 4.5,175L 18,175L 27,175L 36,175L 45,175L 54,175L 63,175" />
<path fill="none" stroke-width="2" d="M 72,175 L 81,175L 90,175L 99,175L 108,175L 117,175L 126,175L 135,175L 144,175L 153,175L 162,175L 171,175L 180,175L 189,175L 198,175L 207,175L 216,175L 225,175L 234,175L 243,175L 252,175L 261,175L 270,175L 279,175L 288,175L 297,175L 306,175Q 310.5,175 310.5,182L 310.5,196L 310.5,210L 310.5,224L 310.5,238L 310.5,252" stroke="green" />
<path stroke-dasharray="5,2" id="t1a" d="M 328.5,182 L 328.5,196L 328.5,210L 328.5,224L 328.5,238L 328.5,252" stroke="blue" stroke-width="2" fill="none" />
<path d="M 445.5,182 L 445.5,196L 445.5,210L 445.5,224L 445.5,238L 445.5,252" fill="none" stroke="green" stroke-width="2" />
<path d="M 481.5,182 L 481.5,196L 481.5,210L 481.5,224L 481.5,238L 481.5,252" stroke-width="2" fill="none" stroke-dasharray="5,2" id="rxb" stroke="blue" />
<path d="M 715.5,189 l -3,0 m 3,-3 l -3,3 l 3,3 m 0,-3L 729,189Q 733.5,189 733.5,196L 733.5,210L 733.5,224L 733.5,238L 733.5,252L 733.5,266L 733.5,280L 733.5,294Q 733.5,301 729,301Q 729,301 720,301L 711,301L 702,301L 693,301L 684,301L 675,301L 666,301L 657,301L 648,301L 639,301L 630,301L 621,301L 612,301L 603,301L 594,301L 585,301L 576,301L 567,301L 558,301L 549,301L 540,301L 531,301L 522,301L 513,301L 504,301L 495,301L 486,301L 477,301L 468,301L 459,301L 450,301L 441,301L 432,301L 423,301L 414,301L 405,301L 396,301L 387,301L 378,301L 369,301L 360,301L 351,301L 342,301L 333,301L 324,301L 315,301L 306,301L 297,301L 288,301L 279,301L 270,301L 261,301L 252,301L 243,301L 234,301L 225,301" id="s2" stroke-width="2" stroke-dasharray="5,2" stroke="black" fill="none" />
<path stroke-width="10" d="M 76.5,224 Q 76.5,231 81,231Q 81,231 90,231L 99,231L 108,231L 117,231L 126,231L 135,231L 144,231L 153,231L 162,231L 171,231L 180,231L 189,231Q 193.5,231 193.5,238L 193.5,252" fill="none" id="m_t2" stroke="orange" />
<path fill="none" d="M 193.5,266 L 193.5,280L 193.5,294L 193.5,308L 193.5,322L 193.5,336L 193.5,350" id="t2a" stroke-dasharray="5,2" stroke="blue" stroke-width="2" />
<path fill="none" stroke="green" d="M 310.5,266 L 310.5,280L 310.5,294" stroke-width="2" />
<path d="M 328.5,266 L 328.5,280L 328.5,294" stroke="blue" stroke-width="2" id="t1b" stroke-dasharray="5,2" fill="none" />
<path stroke="green" stroke-width="2" d="M 445.5,266 L 445.5,280L 445.5,294" fill="none" />
<path d="M 481.5,266 L 481.5,280L 481.5,294" fill="none" id="rxc" stroke="blue" stroke-dasharray="5,2" stroke-width="2" />
<path fill="none" stroke-width="2" d="M 310.5,308 L 310.5,322L 310.5,336L 310.5,350L 310.5,364L 310.5,378L 310.5,392L 310.5,406" stroke="green" />
<path d="M 328.5,308 L 328.5,322L 328.5,336L 328.5,350" stroke-dasharray="5,2" stroke="blue" id="t1c" fill="none" stroke-width="2" />
<path d="M 445.5,308 L 445.5,322L 445.5,336" fill="none" stroke-width="2" stroke="green" />
<path fill="none" stroke-dasharray="5,2" stroke="blue" d="M 486,308 L 495,322L 504,336" stroke-width="2" id="rxd" />
<path d="M 445.5,350 L 445.5,364L 445.5,378" stroke-width="2" fill="none" stroke="green" />
<path stroke-dasharray="5,2" fill="none" stroke="blue" id="rxe" d="M 513,350 L 522,364L 531,378L 540,392L 544.5,399L 544.5,420L 544.5,434" stroke-width="2" />
<path stroke-width="10" d="M 265.5,364 L 265.5,378L 265.5,392L 265.5,406Q 265.5,413 270,413Q 270,413 279,413L 288,413L 297,413L 306,413L 315,413L 324,413L 333,413L 342,413L 351,413L 360,413L 369,413L 378,413L 387,413L 396,413L 405,413L 414,413L 423,413L 432,413L 441,413L 450,413L 459,413L 463.5,413L 477,434" id="m_s2" stroke="blue" fill="none" />
<path id="m_s1" stroke-width="10" fill="none" stroke="blue" d="M 400.5,364 L 400.5,378Q 400.5,385 405,385Q 405,385 414,385L 423,385L 432,385L 441,385L 450,385L 459,385Q 463.5,385 463.5,392L 463.5,406L 463.5,413" />
<path stroke="green" d="M 310.5,420 L 310.5,434L 310.5,448L 310.5,462L 310.5,469L 297,469L 288,469L 279,469L 270,469L 261,469L 252,469L 243,469L 234,469L 225,469L 216,469L 207,469L 198,469L 189,469L 180,469L 175.5,469" stroke-width="2" fill="none" />
<path fill="none" d="M 445.5,420 L 445.5,434L 445.5,448L 445.5,462L 445.5,469L 432,469L 423,469L 414,469L 405,469L 396,469L 387,469L 378,469L 369,469L 360,469L 351,469L 342,469L 333,469L 324,469L 315,469L 310.5,469" stroke="green" stroke-width="2" />
<text xml:space="preserve" text-anchor="start" y="7" dominant-baseline="middle" fill="black" x="9" font-size="14" font-family="Menlo">temp_data: </text>
<text y="35" fill="black" font-family="Menlo" x="9" xml:space="preserve" dominant-baseline="middle" font-size="14" text-anchor="start">(tx, rx) = channel(); </text>
<text y="77" text-anchor="start" font-family="Menlo" dominant-baseline="middle" xml:space="preserve" font-size="14" fill="black" x="477">rx </text>
<text font-family="Menlo" text-anchor="start" xml:space="preserve" y="91" font-size="14" dominant-baseline="middle" x="630" fill="black">'a' </text>
<text font-size="14" dominant-baseline="middle" text-anchor="start" y="119" font-family="Menlo" xml:space="preserve" fill="black" x="630">'b' </text>
<text font-family="Menlo" fill="black" font-size="14" x="18" text-anchor="start" dominant-baseline="middle" y="133" xml:space="preserve">t1 = tx.clone() </text>
<text text-anchor="start" font-family="Menlo" dominant-baseline="middle" x="630" fill="black" font-size="14" y="147" xml:space="preserve">'c' </text>
<text font-family="Menlo" font-size="14" fill="black" text-anchor="start" x="324" dominant-baseline="middle" xml:space="preserve" y="175">t1 , data </text>
<text dominant-baseline="middle" text-anchor="start" font-family="Menlo" y="175" xml:space="preserve" x="630" fill="black" font-size="14">'1' </text>
<text x="630" y="203" font-family="Menlo" xml:space="preserve" font-size="14" text-anchor="start" dominant-baseline="middle" fill="black">'2' </text>
<text dominant-baseline="middle" x="63" y="217" text-anchor="start" fill="black" xml:space="preserve" font-family="Menlo" font-size="14">tx </text>
<text font-family="Menlo" dominant-baseline="middle" xml:space="preserve" x="630" font-size="14" fill="black" y="231" text-anchor="start">'3' </text>
<text font-family="Menlo" font-size="14" xml:space="preserve" y="259" text-anchor="start" dominant-baseline="middle" x="189" fill="black">tx , data </text>
<text fill="black" dominant-baseline="middle" xml:space="preserve" x="207" font-size="14" font-family="Menlo" text-anchor="start" y="301">s </text>
<text font-size="14" text-anchor="start" fill="black" y="343" dominant-baseline="middle" x="360" xml:space="preserve" font-family="Menlo">s </text>
<text fill="black" x="189" y="357" font-size="14" dominant-baseline="middle" font-family="Menlo" text-anchor="start" xml:space="preserve">tx.send(s) </text>
<text text-anchor="start" y="357" font-size="14" xml:space="preserve" font-family="Menlo" fill="black" dominant-baseline="middle" x="324">t1.send(s) </text>
<text xml:space="preserve" fill="black" text-anchor="start" y="399" font-family="Menlo" dominant-baseline="middle" font-size="14" x="441">| </text>
<text font-size="14" xml:space="preserve" y="441" font-family="Menlo" dominant-baseline="middle" x="459" text-anchor="start" fill="black">Ok(s) = rx.recv()</text>
<text y="455" fill="black" dominant-baseline="middle" xml:space="preserve" x="459" text-anchor="start" font-family="Menlo" font-size="14">println!("A1 received {:?}", s)</text>
</svg>
<ul>
<li class="fragment"><code class="sourceCode rust">rx</code>, <code class="sourceCode rust">t<span class="dv">1</span></code>, <code class="sourceCode rust">tx</code> still moved from one thread to another.</li>
<li class="fragment"><code class="sourceCode rust">data</code> is shared reference to character slice: freely copyable</li>
<li class="fragment"><code>s</code>'s are subslices <code class="sourceCode rust">&data<span class="op">[</span><span class="dv">0.</span>.<span class="dv">4</span><span class="op">]</span></code> + <code class="sourceCode rust">&data<span class="op">[</span><span class="dv">2.</span>.<span class="dv">6</span><span class="op">]</span></code> (overlap is safe!)</li>
</ul>
</section>
<section id="sending-and-sharing-intertwine" class="slide level2">
<h2>Sending and sharing intertwine</h2>
<p>Can send <code>&</code>-references and <code>&mut</code>-references</p>
<ul>
<li class="fragment"><code>&</code>-refs copy (as usual).</li>
<li class="fragment"><code>&mut</code>-refs obey move semantics when sent (as usual)</li>
</ul>
<p>Examples</p>
<div class="sourceCode"><pre class="sourceCode rust"><code class="sourceCode rust"><span class="kw">fn</span> send_ref_i32(arg: &<span class="dt">i32</span>) <span class="op">{</span>
rayon::scope(<span class="kw">move</span> |s| <span class="op">{</span>
s.spawn(<span class="kw">move</span> |_s| <span class="pp">println!</span>(<span class="st">"arg: {:?}"</span>, arg));
<span class="op">}</span>);
<span class="op">}</span>
<span class="kw">fn</span> send_ref_vec(arg: &<span class="dt">Vec</span><<span class="dt">i32</span>>) <span class="op">{</span>
rayon::scope(<span class="kw">move</span> |s| <span class="op">{</span>
s.spawn(<span class="kw">move</span> |_s| <span class="pp">println!</span>(<span class="st">"arg: {:?}"</span>, arg));
<span class="op">}</span>);
<span class="op">}</span>
<span class="kw">fn</span> send_mut_vec(arg: &<span class="kw">mut</span> <span class="dt">Vec</span><<span class="dt">i32</span>>) <span class="op">{</span>
rayon::scope(<span class="kw">move</span> |s| <span class="op">{</span>
s.spawn(<span class="kw">move</span> |_s| <span class="pp">println!</span>(<span class="st">"arg: {:?}"</span>, arg));
<span class="op">}</span>);
<span class="op">}</span></code></pre></div>
<p>So far so good</p>
</section>
<section id="cant-always-get-what-you-want-..." class="slide level2">
<h2>"Can't always get what you want ..."</h2>
<p>Can send <code>&</code>-references and <code>&mut</code>-references ... if data synchronized!</p>
<div class="sourceCode"><pre class="sourceCode rust compile_error"><code class="sourceCode rust"><span class="kw">fn</span> send_ref_to_cell(arg: &Cell<<span class="dt">i32</span>>) <span class="op">{</span>
rayon::scope(<span class="kw">move</span> |s| <span class="op">{</span>
s.spawn(<span class="kw">move</span> |_s| <span class="pp">println!</span>(<span class="st">"arg: {:?}"</span>, arg));
<span class="op">}</span>);
<span class="op">}</span></code></pre></div>
<div class="fragment">
<pre class="terminal"><code>error[E0277]: the trait bound `Cell<i32>: Sync` is not satisfied
--> src/a00.rs:547:5
|
547 | rayon::scope(move |s| {
| ^^^^^^^^^^^^ `Cell<i32>` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `Cell<i32>`
= note: required because of the requirements on the impl of `Send` for `&Cell<i32>`
= note: required because it appears within the type `[closure@src/a00.rs:547:18: 549:6 arg:&Cell<i32>]`
= note: required by `rayon::scope`</code></pre>
</div>
<div class="fragment">
<p>"but [...] you get what you need"</p>
<p><code class="sourceCode rust">Cell<T></code>: <em>unsynchronized</em> mutation; incompatible with <code>Sync</code>.</p>
<p>(Synchronous alternatives include <code class="sourceCode rust">AtomicUsize</code>, <code>Mutex<T></code>)</p>
</div>
</section>
<section id="the-send-traits" class="slide level2">
<h2>The <code class="sourceCode rust"><span class="bu">Send</span></code> traits</h2>
<p><code class="sourceCode rust"><span class="bu">Send</span></code>: focused on ownership transfer</p>
<div class="fragment">
<p>But we already know that move semantics alone does not suffice</p>
<p>(We need our references!)</p>
</div>
</section>
<section id="the-send-and-sync-traits" class="slide level2">
<h2>The <code class="sourceCode rust"><span class="bu">Send</span></code> and <code class="sourceCode rust"><span class="bu">Sync</span></code> traits</h2>
<ul>
<li class="fragment"><code class="sourceCode rust"><span class="bu">Send</span></code> and <code class="sourceCode rust"><span class="bu">Sync</span></code> control cross-thread capabilities</li>
<li class="fragment"><code class="sourceCode rust">T: <span class="bu">Send</span></code> implies ownership of <code class="sourceCode rust">T</code> can be tranferred across threads</li>
<li class="fragment"><code class="sourceCode rust">T: <span class="bu">Sync</span></code> implies a reference to <code class="sourceCode rust">T</code> (e.g. <code class="sourceCode rust">&T</code>, <code class="sourceCode rust">Arc<T></code>) can be shared across threads</li>
<li class="fragment"><code class="sourceCode rust"><span class="bu">Send</span></code> enables Message Passing style concurrency, via channels</li>
<li class="fragment"><code class="sourceCode rust"><span class="bu">Sync</span></code> enables Shared State style concurrency; only <code class="sourceCode rust">T</code> with synchronized access is allowed</li>
<li class="fragment">Rust compiler <em>automatically</em> marks types as <code class="sourceCode rust"><span class="bu">Send</span></code> or <code class="sourceCode rust"><span class="bu">Sync</span></code>, based on a recursive analysis of their structure.</li>
<li class="fragment">Data-structure designer can opt out of above analysis. (If doing <code class="sourceCode rust"><span class="kw">unsafe</span></code> stuff, they may need to do so for soundness!)</li>
</ul>
<p>See also: <a href="https://www.ralfj.de/blog/2017/06/09/mutexguard-sync.html"><code>https://www.ralfj.de/blog/2017/06/09/mutexguard-sync.html</code></a></p>
</section>
<section id="rust-as-enabler-of-individuals" class="slide level2 big_text center">
<h2>Rust as enabler of individuals</h2>
<h3 id="from-mere-script-programmer">From "mere script programmer"</h3>
<h3 id="to-lauded-systems-hacker">to "lauded systems hacker"</h3>
<div class="fragment">
<blockquote>
<p>Programming in Rust has made me look at C++ code in a whole new light</p>
</blockquote>
</div>
<div class="fragment">
<blockquote>
<p>After experiencing Rust, I dread looking at code from prior projects ... I will now see how riddled with races it was</p>
</blockquote>
</div>
</section>
<section id="thanks" class="slide level2">
<h2>Thanks</h2>
<div class="no_border logo">
<img src="Rust_programming_language_black_logo.svg" />
<div>
<table>
<tbody>
<tr class="odd">
<td><code>www.rust-lang.org</code></td>
<td>Hack Without Fear</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
// Push each slide change to the browser history
history: true,
// Vertical centering of slides
center: true,
// Optional reveal.js plugins
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: 'reveal.js/plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>