-
Notifications
You must be signed in to change notification settings - Fork 0
/
slides.html
1568 lines (1288 loc) · 88.7 KB
/
slides.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 lang="en">
<head>
<meta charset="utf-8">
<meta name="generator" content="rustdoc">
<title>Rust Workshop</title>
<link rel="stylesheet" href="css/rust.css">
<link rel="stylesheet" href="css/sliderust.css">
<script src="js/sliderust.js"></script>
</head>
<body class="rustdoc">
<!--[if lte IE 8]>
<div class="warning">
This old browser is unsupported and will most likely display funky
things.
</div>
<![endif]-->
<h1 class="title">Rust Workshop</h1>
<p>Rust Romandie meetup + Hackers at EPFL</p>
<p>Dimiter Petrov & Romain Ruetschi</p>
<p>November 12, 2014</p>
<h1 id="hello,-rust" class='section-header'><a
href="#hello,-rust">Hello, Rust</a></h1>
<p>Rust is a fairly new systems programming language focused on safety and speed.</p>
<pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Hello, world!"</span>);
}
</pre>
<pre><code class="language-norust">> Hello, world!
</code></pre>
<h1 id="history" class='section-header'><a
href="#history">History</a></h1>
<ul>
<li>Started as a personal project in 2006 by Graydon Hoare</li>
<li>First compiler written in OCaml</li>
<li>Mozilla took over the project 3 years later</li>
<li>Compiler rewritten in Rust itself in 2010</li>
</ul>
<h1 id="why-rust-at-mozilla?" class='section-header'><a
href="#why-rust-at-mozilla?">Why Rust at Mozilla?</a></h1>
<ul>
<li>New experimental, highly parallel browser engine: Servo</li>
<li>Browsers evolve in a hostile, highly concurrent environment</li>
<li>Most security problems arise from memory safetey and concurrency issues</li>
<li>Thus a safer, saner systems language is needed, with built-in concurrency</li>
</ul>
<h1 id="why-rust,-for-you?" class='section-header'><a
href="#why-rust,-for-you?">Why Rust, for you?</a></h1>
<ul>
<li>High-level features</li>
<li>Low-level control</li>
<li>Powerful Foreign Function Interface (FFI)</li>
<li>Direct memory control</li>
<li>Guaranteed memory safety</li>
<li>Concurrency without data races</li>
<li>Zero cost abstractions</li>
</ul>
<h1 id="high-level-features" class='section-header'><a
href="#high-level-features">High-level features</a></h1>
<ul>
<li>Type inference</li>
<li>Lambdas</li>
<li>Closures</li>
<li>Iterators</li>
<li>Algebraic data types</li>
<li>Pattern matching</li>
<li>Traits (= type classes)</li>
<li>Macros</li>
<li>Modules</li>
</ul>
<h1 id="low-level-control" class='section-header'><a
href="#low-level-control">Low-level control</a></h1>
<ul>
<li>Ability to trade compiler-enforced safety for lower-level control</li>
<li>Eg. dereferencing raw pointer, pointer arithmetic, calling external code</li>
<li>Such unsafe code must be surrounded by an <code>unsafe</code> block</li>
<li>Thus easy to spot during code review or when debugging</li>
</ul>
<h1 id="powerful-ffi" class='section-header'><a
href="#powerful-ffi">Powerful FFI</a></h1>
<ul>
<li>Call Rust code from other languages (C, C++, or even GCed languages like Ruby)</li>
<li>Create bindings for libraries exposing a C interface</li>
</ul>
<h1 id="direct-memory-control" class='section-header'><a
href="#direct-memory-control">Direct memory control</a></h1>
<ul>
<li>No garbage collector</li>
<li>No runtime overhead</li>
<li>No unpredictable pauses in execution</li>
<li>Stack and heap allocations</li>
</ul>
<h1 id="but-first,-the-basics" class='section-header'><a
href="#but-first,-the-basics">But first, the basics</a></h1>
<ul>
<li>Primitive types</li>
<li>Variable bindings</li>
<li>Functions</li>
<li>Printing stuff</li>
<li>Control structures</li>
<li>Strings</li>
<li>Arrays, vectors and slices</li>
<li>Structs and methods</li>
</ul>
<h1 id="numeric-types" class='section-header'><a
href="#numeric-types">Numeric types</a></h1>
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Example</th>
</tr>
<tr>
<td>Signed integer</td>
<td><code>int</code></td>
<td><code>5i</code></td>
</tr>
<tr>
<td>Unsigned integer</td>
<td><code>uint</code></td>
<td><code>5u</code></td>
</tr>
<tr>
<td>8-bit uint</td>
<td><code>u8</code></td>
<td><code>5u8</code></td>
</tr>
<tr>
<td>16-bit uint</td>
<td><code>u16</code></td>
<td><code>5i</code></td>
</tr>
<tr>
<td>etc.</td>
<td></td>
<td></td>
</tr>
<tr>
<td>32-bit float</td>
<td><code>f32</code></td>
<td><code>3.14f32</code></td>
</tr>
<tr>
<td>64-bit float</td>
<td><code>f64</code></td>
<td><code>3.14_f64</code></td>
</tr>
</table>
<h1 id="primitive-types" class='section-header'><a
href="#primitive-types">Primitive types</a></h1>
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Example</th>
<th></th>
</tr>
<tr>
<td>Unit</td>
<td><code>()</code></td>
<td><code>()</code></td>
<td></td>
</tr>
<tr>
<td>Boolean</td>
<td><code>bool</code></td>
<td><code>true | false</code></td>
<td></td>
</tr>
<tr>
<td>Array</td>
<td><code>[T]</code></td>
<td><code>[1, 2, 3]</code>
<td>fixed size, can be allocated on either the stack or the heap</td>
</tr>
<tr>
<td>Slice</td>
<td><code>&[T]</code></td>
<td><code>&[1, 2, 3]</code>
<td>'view' into an array, doesn't own the data it points to, but borrows it</td>
</tr>
<tr>
<td>Tuple</td>
<td><code>(A, B, C...)</code></td>
<td><code>("Rust", 2006i, 'r')</code></td>
<td>32-bit unsigned word</td>
</tr>
</table>
<h1 id="textual-types" class='section-header'><a
href="#textual-types">Textual types</a></h1>
<table>
<tr>
<th>Name</th>
<th>Type</th>
<th>Example</th>
<th></th>
</tr>
<tr>
<td>Unicode scalar value</td>
<td><code>char</code></td>
<td><code>'a'</code></td>
<td>32-bit unsigned word</td>
</tr>
<tr>
<td>Unicode scalar array</td>
<td><code>[char]</code></td>
<td><code>['a', 'b', 'c']</code></td>
<td>~ UTF-32 string</td>
</tr>
<tr>
<td>Unicode string</td>
<td><code>str</code></td>
<td><code>"rust is cool"</code></td>
<td>array of 8-bit unsigned bytes<br>
~ sequence of UTF-8 codepoints</td>
</tr>
</table>
<h1 id="variable-bindings" class='section-header'><a
href="#variable-bindings">Variable bindings</a></h1><pre class='rust '>
<span class='kw'>let</span> <span class='ident'>x</span>: <span class='ident'>int</span> <span class='op'>=</span> <span class='number'>5</span>;
<span class='comment'>// type annotations are (usually) optional</span>
<span class='kw'>let</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='number'>5i</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>y</span> <span class='op'>=</span> <span class='number'>2i</span>;
<span class='ident'>y</span> <span class='op'>+=</span> <span class='number'>1</span>;
</pre>
<h1 id="mutability" class='section-header'><a
href="#mutability">Mutability</a></h1><pre class='rust '>
<span class='kw'>let</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='number'>5i</span>;
<span class='ident'>x</span> <span class='op'>=</span> <span class='ident'>x</span> <span class='op'>+</span> <span class='number'>1</span>; <span class='comment'>// error</span>
</pre>
<h1 id="mutability-1" class='section-header'><a
href="#mutability-1">Mutability</a></h1><pre class='rust '>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='number'>5i</span>;
<span class='ident'>x</span> <span class='op'>=</span> <span class='ident'>x</span> <span class='op'>+</span> <span class='number'>1</span>; <span class='comment'>// ok</span>
</pre>
<h1 id="mutability-2" class='section-header'><a
href="#mutability-2">Mutability</a></h1><pre class='rust '>
<span class='kw'>let</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='number'>5i</span>;
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>y</span> <span class='op'>=</span> <span class='ident'>x</span>;
<span class='ident'>y</span> <span class='op'>=</span> <span class='ident'>y</span> <span class='op'>+</span> <span class='number'>1</span>;
</pre>
<h1 id="functions" class='section-header'><a
href="#functions">Functions</a></h1><pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>res</span> <span class='op'>=</span> <span class='ident'>do_stuff</span>(<span class='number'>2</span>, <span class='number'>3</span>);
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"result is {}"</span>, <span class='ident'>res</span>);
}
<span class='kw'>fn</span> <span class='ident'>do_stuff</span>(<span class='ident'>a</span>: <span class='ident'>int</span>, <span class='ident'>b</span>: <span class='ident'>int</span>) <span class='op'>-></span> <span class='ident'>int</span> {
<span class='kw'>let</span> <span class='ident'>c</span> <span class='op'>=</span> <span class='ident'>a</span> <span class='op'>+</span> <span class='ident'>b</span>;
<span class='kw'>return</span> <span class='ident'>c</span> <span class='op'>*</span> <span class='number'>2</span>;
}
</pre>
<h1 id="functions-1" class='section-header'><a
href="#functions-1">Functions</a></h1>
<p>You can omit <code>return</code> if you want to return the last expression.</p>
<pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>res</span> <span class='op'>=</span> <span class='ident'>do_stuff</span>(<span class='number'>2</span>, <span class='number'>3</span>);
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"result is {}"</span>, <span class='ident'>res</span>);
}
<span class='kw'>fn</span> <span class='ident'>do_stuff</span>(<span class='ident'>a</span>: <span class='ident'>int</span>, <span class='ident'>b</span>: <span class='ident'>int</span>) <span class='op'>-></span> <span class='ident'>int</span> {
<span class='kw'>let</span> <span class='ident'>c</span> <span class='op'>=</span> <span class='ident'>a</span> <span class='op'>+</span> <span class='ident'>b</span>;
<span class='ident'>c</span> <span class='op'>*</span> <span class='number'>2</span> <span class='comment'>// ! no semicolon</span>
}
</pre>
<p>But you also need to omit the last semicolon, otherwise the function will return <code>()</code>.</p>
<h1 id="printing-stuff" class='section-header'><a
href="#printing-stuff">Printing stuff</a></h1><pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>name</span> <span class='op'>=</span> <span class='string'>"John"</span>;
<span class='kw'>let</span> <span class='ident'>age</span> <span class='op'>=</span> <span class='number'>42</span>;
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{} is {}"</span>, <span class='ident'>name</span>, <span class='ident'>age</span>);
}
</pre>
<blockquote>
<p><code>println!()</code> is actually a macro, not a function.</p>
</blockquote>
<h1 id="printing-stuff-1" class='section-header'><a
href="#printing-stuff-1">Printing stuff</a></h1>
<p>You can re-order arguments by putting their index inside the braces.</p>
<pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>name</span> <span class='op'>=</span> <span class='string'>"John"</span>;
<span class='kw'>let</span> <span class='ident'>age</span> <span class='op'>=</span> <span class='number'>42</span>;
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{1} is {0}"</span>, <span class='ident'>age</span>, <span class='ident'>name</span>);
}
</pre>
<h1 id="conditionals" class='section-header'><a
href="#conditionals">Conditionals</a></h1><pre class='rust '>
<span class='kw'>if</span> <span class='ident'>age</span> <span class='op'>></span> <span class='number'>16</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Have a beer!"</span>);
}
<span class='kw'>else</span> {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"Sorry, no beer for you."</span>);
}
</pre>
<blockquote>
<p>No parenthesis around the condition.</p>
</blockquote>
<h1 id="conditionals-1" class='section-header'><a
href="#conditionals-1">Conditionals</a></h1>
<p><code>if/else</code> is an expression:</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='ident'>message</span> <span class='op'>=</span> <span class='kw'>if</span> <span class='ident'>health</span> <span class='op'>></span> <span class='number'>0</span> {
<span class='string'>"Good job"</span>
} <span class='kw'>else</span> {
<span class='string'>"Game over"</span>
};
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{}"</span>, <span class='ident'>message</span>);
</pre>
<h1 id="while" class='section-header'><a
href="#while">While</a></h1><pre class='rust '>
<span class='kw'>while</span> <span class='ident'>x</span> <span class='op'><</span> <span class='number'>10</span> {
<span class='comment'>// do stuff</span>
}
</pre>
<h1 id="loop" class='section-header'><a
href="#loop">Loop</a></h1>
<p>Instead of</p>
<pre class='rust '>
<span class='kw'>while</span> <span class='boolval'>true</span> {
<span class='comment'>// do stuff</span>
}
</pre>
<p>use</p>
<pre class='rust '>
<span class='kw'>loop</span> {
<span class='comment'>// do stuff</span>
}
</pre>
<blockquote>
<p>Rust's control-flow analysis treats this construct differently than a <code>while true</code>, which leads to better generated code.</p>
</blockquote>
<h1 id="for" class='section-header'><a
href="#for">For</a></h1><pre class='rust '>
<span class='kw'>for</span> <span class='ident'>x</span> <span class='kw'>in</span> <span class='ident'>range</span>(<span class='number'>0i</span>, <span class='number'>10i</span>) {
<span class='comment'>// do stuff</span>
}
</pre>
<h1 id="for-1" class='section-header'><a
href="#for-1">For</a></h1><pre class='rust '>
<span class='kw'>for</span> <span class='ident'>var</span> <span class='kw'>in</span> <span class='ident'>expression</span> {
<span class='ident'>code</span>
}
</pre>
<blockquote>
<p><code>expression</code> must be an <code>Iterator</code> (we'll talk about it later).</p>
</blockquote>
<h1 id="strings" class='section-header'><a
href="#strings">Strings</a></h1>
<ul>
<li>Guaranteed to be validly-encoded UTF-8 sequences</li>
<li>Not null-terminated (can contain null bytes)</li>
<li>Can be constant strings (<code>&str</code>) or growable strings (<code>String</code>)</li>
</ul>
<h1 id="string-slices-(&str)" class='section-header'><a
href="#string-slices-(&str)">String slices (<code>&str</code>)</a></h1>
<p>A pointer and a length.
String slices are a 'view' into already allocated strings like string literals:</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='ident'>hello</span> <span class='op'>=</span> <span class='string'>"Hello, world!"</span>;
</pre>
<h1 id="growable-strings-(string)" class='section-header'><a
href="#growable-strings-(string)">Growable strings (<code>String</code>)</a></h1>
<p><code>String</code> can grow if defined as <code>mut</code>able.</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>hello</span> <span class='op'>=</span> <span class='string'>"Hello"</span>.<span class='ident'>to_string</span>();
<span class='ident'>hello</span>.<span class='ident'>push_str</span>(<span class='string'>", world!"</span>);
<span class='comment'>// hello now contains "Hello, world!"</span>
</pre>
<h1 id="conversions-between-the-two" class='section-header'><a
href="#conversions-between-the-two">Conversions between the two</a></h1>
<ul>
<li>Call <code>as_slice()</code> on a <code>String</code> to convert it to a <code>&str</code></li>
<li>Call <code>to_string()</code> on a <code>&str</code> to convert it to a <code>String</code></li>
</ul>
<pre class='rust '>
<span class='kw'>let</span> <span class='ident'>s1</span>: <span class='ident'>String</span> <span class='op'>=</span> <span class='string'>"Hello"</span>.<span class='ident'>to_string</span>();
<span class='kw'>let</span> <span class='ident'>s2</span>: <span class='kw-2'>&</span><span class='ident'>str</span> <span class='op'>=</span> <span class='ident'>s1</span>.<span class='ident'>as_slice</span>();
</pre>
<p>Converting <code>String</code> to <code>&str</code> is cheap, converting <code>&str</code> to <code>String</code> involves an allocation.</p>
<h1 id="arrays,-vectors-and-slices" class='section-header'><a
href="#arrays,-vectors-and-slices">Arrays, vectors and slices</a></h1>
<ul>
<li>An array is a <strong>fixed-sized</strong> list of elements of the same type</li>
<li>A vector is a <strong>dynamic, growable</strong> array</li>
<li>A slice is a reference to an array.</li>
</ul>
<h1 id="arrays" class='section-header'><a
href="#arrays">Arrays</a></h1>
<p>An array is a <strong>fixed-sized</strong> list of elements of the same type.</p>
<pre class='rust '>
<span class='comment'>// Only the first item needs a type suffix</span>
<span class='kw'>let</span> <span class='ident'>a</span> <span class='op'>=</span> [<span class='number'>1i</span>, <span class='number'>2</span>, <span class='number'>3</span>];
<span class='comment'>// which you can omit if you specify the type</span>
<span class='kw'>let</span> <span class='ident'>a</span>: [<span class='ident'>int</span>, ..<span class='number'>3</span>] <span class='op'>=</span> [<span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>];
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"a has {} elements"</span>, <span class='ident'>a</span>.<span class='ident'>len</span>());
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"the first element of a is {}"</span>, <span class='ident'>a</span>[<span class='number'>0</span>]);
</pre>
<h1 id="vectors" class='section-header'><a
href="#vectors">Vectors</a></h1>
<p>A vector is a <strong>dynamic, growable</strong> array</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>participants</span> <span class='op'>=</span> <span class='macro'>vec</span><span class='macro'>!</span>[<span class='string'>"Bob"</span>, <span class='string'>"Bill"</span>];
<span class='ident'>participants</span>.<span class='ident'>push</span>(<span class='string'>"Joe"</span>);
<span class='comment'>// participants[2] == "Joe";</span>
</pre>
<h1 id="slices" class='section-header'><a
href="#slices">Slices</a></h1>
<p>A slice is a reference to an array.</p>
<p>A slice allows safe and efficient access to a portion of an array without copying.</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='ident'>a</span>: [<span class='ident'>int</span>, ..<span class='number'>5</span>] <span class='op'>=</span> [<span class='number'>0</span>, <span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>, <span class='number'>4</span>];
<span class='kw'>let</span> <span class='ident'>middle</span>: <span class='kw-2'>&</span>[<span class='ident'>int</span>] <span class='op'>=</span> <span class='ident'>a</span>.<span class='ident'>slice</span>(<span class='number'>1</span>, <span class='number'>4</span>); <span class='comment'>// just the elements [1, 2, 3]</span>
<span class='kw'>let</span> <span class='ident'>a</span> <span class='op'>=</span> [<span class='number'>0i</span>, <span class='number'>1</span>, <span class='number'>2</span>, <span class='number'>3</span>, <span class='number'>4</span>];
<span class='kw'>let</span> <span class='ident'>middle</span> <span class='op'>=</span> <span class='ident'>a</span>.<span class='ident'>slice</span>(<span class='number'>1</span>, <span class='number'>4</span>); <span class='comment'>// just the elements [1, 2, 3]</span>
</pre>
<h1 id="structs-and-methods" class='section-header'><a
href="#structs-and-methods">Structs and methods</a></h1>
<h1 id="structs" class='section-header'><a
href="#structs">Structs</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Rectangle</span> {
<span class='ident'>width</span>: <span class='ident'>f32</span>,
<span class='ident'>height</span>: <span class='ident'>f32</span>
}
</pre>
<h1 id="methods" class='section-header'><a
href="#methods">Methods</a></h1>
<p>Methods (static and members) are defined inside an <code>impl</code> block.</p>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Rectangle</span> {
<span class='ident'>width</span>: <span class='ident'>f32</span>,
<span class='ident'>height</span>: <span class='ident'>f32</span>
}
<span class='kw'>impl</span> <span class='ident'>Rectangle</span> {
<span class='comment'>// Methods here.</span>
}
</pre>
<h1 id="static-methods" class='section-header'><a
href="#static-methods">Static methods</a></h1>
<p>Static methods are defined this way, and can be called with <code>Type::method_name()</code>.</p>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Rectangle</span> { <span class='ident'>width</span>: <span class='ident'>f32</span>, <span class='ident'>height</span>: <span class='ident'>f32</span> }
<span class='kw'>impl</span> <span class='ident'>Rectangle</span> {
<span class='kw'>fn</span> <span class='ident'>new</span>(<span class='ident'>width</span>: <span class='ident'>f32</span>, <span class='ident'>height</span>: <span class='ident'>f32</span>) <span class='op'>-></span> <span class='ident'>Rectangle</span> {
<span class='ident'>Rectangle</span> { <span class='ident'>width</span>: <span class='ident'>width</span>, <span class='ident'>height</span>: <span class='ident'>height</span> }
}
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>rect</span> <span class='op'>=</span> <span class='ident'>Rectangle</span>::<span class='ident'>new</span>(<span class='number'>1.2</span>, <span class='number'>4.9</span>);
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{}"</span>, <span class='ident'>react</span>.<span class='ident'>area</span>());
}
</pre>
<h1 id="member-methods" class='section-header'><a
href="#member-methods">Member methods</a></h1>
<p>Member methods take a <code>&self</code> parameter, which is a reference to the <code>struct</code> the method is called on.</p>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Rectangle</span> { <span class='ident'>width</span>: <span class='ident'>f32</span>, <span class='ident'>height</span>: <span class='ident'>f32</span> }
<span class='kw'>impl</span> <span class='ident'>Rectangle</span> {
<span class='kw'>fn</span> <span class='ident'>area</span>(<span class='kw-2'>&</span><span class='self'>self</span>) <span class='op'>-></span> <span class='ident'>f32</span> {
<span class='self'>self</span>.<span class='ident'>width</span> <span class='op'>*</span> <span class='self'>self</span>.<span class='ident'>height</span>
}
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>rect</span> <span class='op'>=</span> <span class='ident'>Rectangle</span>::<span class='ident'>new</span>(<span class='number'>1.2</span>, <span class='number'>4.9</span>);
<span class='kw'>let</span> <span class='ident'>area</span> <span class='op'>=</span> <span class='ident'>react</span>.<span class='ident'>area</span>();
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{}"</span>, <span class='ident'>area</span>);
}
</pre>
<h1 id="generics" class='section-header'><a
href="#generics">Generics</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>PairOfInts</span> { <span class='ident'>first</span>: <span class='ident'>int</span>, <span class='ident'>second</span>: <span class='ident'>int</span> }
<span class='kw'>fn</span> <span class='ident'>swap_pair</span>(<span class='ident'>p</span>: <span class='ident'>PairOfInts</span>) <span class='op'>-></span> <span class='ident'>PairOfInts</span> {
<span class='ident'>PairOfInts</span> { <span class='ident'>first</span>: <span class='ident'>p</span>.<span class='ident'>second</span>, <span class='ident'>second</span>: <span class='ident'>p</span>.<span class='ident'>first</span> }
}
</pre>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Pair</span><span class='op'><</span><span class='ident'>T</span><span class='op'>></span> { <span class='ident'>first</span>: <span class='ident'>T</span>, <span class='ident'>second</span>: <span class='ident'>T</span> }
<span class='kw'>fn</span> <span class='ident'>swap_pair</span><span class='op'><</span><span class='ident'>T</span><span class='op'>></span>(<span class='ident'>p</span>: <span class='ident'>Pair</span><span class='op'><</span><span class='ident'>T</span><span class='op'>></span>) <span class='op'>-></span> <span class='ident'>Pair</span><span class='op'><</span><span class='ident'>T</span><span class='op'>></span> {
<span class='ident'>Pair</span> { <span class='ident'>first</span>: <span class='ident'>p</span>.<span class='ident'>second</span>, <span class='ident'>second</span>: <span class='ident'>p</span>.<span class='ident'>first</span> }
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>p</span> <span class='op'>=</span> <span class='ident'>Pair</span> { <span class='ident'>first</span>: <span class='number'>1i</span>, <span class='ident'>second</span>: <span class='number'>2i</span> };
<span class='kw'>let</span> <span class='ident'>swapped</span> <span class='op'>=</span> <span class='ident'>swap_pair</span>(<span class='ident'>p</span>);
}
</pre>
<h1 id="stack-and-heap-allocations" class='section-header'><a
href="#stack-and-heap-allocations">Stack and heap allocations</a></h1>
<p>By default, values are allocated on the stack.</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='ident'>rect</span> <span class='op'>=</span> <span class='ident'>Rectangle</span> {
<span class='ident'>width</span>: <span class='number'>1.0</span>,
<span class='ident'>height</span>: <span class='number'>1.0</span>
};
</pre>
<blockquote>
<p><code>rect</code> is of type <code>Rectangle</code></p>
</blockquote>
<h1 id="stack-and-heap-allocations-1" class='section-header'><a
href="#stack-and-heap-allocations-1">Stack and heap allocations</a></h1>
<p>You can allocate a value on the heap with the <code>box</code> keyword.</p>
<h1 id="stack-and-heap-allocations-2" class='section-header'><a
href="#stack-and-heap-allocations-2">Stack and heap allocations</a></h1><pre class='rust '>
{
<span class='kw'>let</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='kw'>box</span> <span class='number'>5i</span>;
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{}"</span>, <span class='op'>*</span><span class='ident'>x</span>); <span class='comment'>// Prints 5</span>
}
</pre>
<pre class='rust '>
{
<span class='ident'>int</span> <span class='op'>*</span><span class='ident'>x</span> <span class='op'>=</span> (<span class='ident'>int</span> <span class='op'>*</span>)<span class='ident'>malloc</span>(<span class='kw'>sizeof</span>(<span class='ident'>int</span>));
<span class='kw'>if</span> (<span class='op'>!</span><span class='ident'>x</span>) <span class='ident'>abort</span>();
<span class='op'>*</span><span class='ident'>x</span> <span class='op'>=</span> <span class='number'>5</span>;
<span class='ident'>printf</span>(<span class='string'>"%d\n"</span>, <span class='op'>*</span><span class='ident'>x</span>);
<span class='ident'>free</span>(<span class='ident'>x</span>);
}
</pre>
<h1 id="boxes" class='section-header'><a
href="#boxes">Boxes</a></h1><pre class='rust '>
<span class='kw'>let</span> <span class='ident'>rect</span> <span class='op'>=</span> <span class='kw'>box</span> <span class='ident'>Rectangle</span> {
<span class='ident'>width</span>: <span class='number'>1.0</span>,
<span class='ident'>height</span>: <span class='number'>1.0</span>
};
</pre>
<blockquote>
<p><code>rect</code> is now type <code>Box<Rectangle></code></p>
</blockquote>
<h1 id="boxes-1" class='section-header'><a
href="#boxes-1">Boxes</a></h1>
<p>Although it's of type <code>Box<Rectangle></code>, you can still call <code>Rectangle</code> methods on <code>rect</code>.</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='ident'>rect</span> <span class='op'>=</span> <span class='kw'>box</span> <span class='ident'>Rectangle</span>::<span class='ident'>new</span>(<span class='number'>1.0</span>, <span class='number'>1.0</span>);
<span class='kw'>let</span> <span class='ident'>area</span> <span class='op'>=</span> <span class='ident'>rect</span>.<span class='ident'>area</span>();
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"area = {}"</span>, <span class='ident'>area</span>);
</pre>
<h1 id="guaranteed-memory-safety" class='section-header'><a
href="#guaranteed-memory-safety">Guaranteed memory safety</a></h1>
<ul>
<li>No buffer overflows</li>
<li>No dangling pointers</li>
</ul>
<h1 id="concurrency-without-data-races" class='section-header'><a
href="#concurrency-without-data-races">Concurrency without data races</a></h1>
<ul>
<li>Only immutable data can be shared</li>
<li>Shared data must remain live as long as any task might access it</li>
</ul>
<h1 id="zero-cost-abstractions" class='section-header'><a
href="#zero-cost-abstractions">Zero cost abstractions</a></h1>
<p>Memory safety is enforced during compilation, there is no runtime overhead.</p>
<h1 id="but-how?" class='section-header'><a
href="#but-how?">But how?</a></h1>
<p>Rust is built around 3 concepts:</p>
<ul>
<li>Lifetime</li>
<li>Ownership</li>
<li>Borrowing</li>
</ul>
<h1 id="lifetime" class='section-header'><a
href="#lifetime">Lifetime</a></h1>
<p>Time span during which a resource (value) is valid.</p>
<h1 id="lifetime-1" class='section-header'><a
href="#lifetime-1">Lifetime</a></h1>
<p><img src="http://cl.ly/image/343C1X3o3K1s/Screen%20Shot%202014-11-10%20at%2018.06.58.png" alt=""></p>
<ul>
<li>You can think of it as a scope.</li>
<li>At the end of its lifetime, a value will be deallocated.</li>
</ul>
<h1 id="lifetime-2" class='section-header'><a
href="#lifetime-2">Lifetime</a></h1>
<pre><code class="language-c">{
int *x = malloc(sizeof(int));
*x = 5;
free(x);
}
</code></pre>
<h1 id="lifetime-3" class='section-header'><a
href="#lifetime-3">Lifetime</a></h1><pre class='rust '>
{
<span class='kw'>let</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='kw'>box</span> <span class='number'>5i</span>;
<span class='comment'>// x gets deallocated here</span>
}
</pre>
<h1 id="move-&-copy" class='section-header'><a
href="#move-&-copy">Move & Copy</a></h1>
<p>When you pass data to a function (or even assign it to a variable), that data can be copied, moved, or borrowed (more about it soon).</p>
<h1 id="copy" class='section-header'><a
href="#copy">Copy</a></h1><pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>a</span> <span class='op'>=</span> <span class='number'>5</span>;
<span class='kw'>let</span> <span class='ident'>b</span> <span class='op'>=</span> <span class='ident'>add_one</span>(<span class='ident'>a</span>);
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{}"</span>, <span class='ident'>a</span>);
}
<span class='kw'>fn</span> <span class='ident'>add_one</span>(<span class='ident'>x</span>: <span class='ident'>int</span>) <span class='op'>-></span> <span class='ident'>int</span> {
<span class='ident'>x</span> <span class='op'>+</span> <span class='number'>1</span>
}
</pre>
<h1 id="move" class='section-header'><a
href="#move">Move</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Person</span> {
<span class='ident'>name</span>: <span class='ident'>String</span>,
<span class='ident'>age</span>: <span class='ident'>uint</span>
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='ident'>show</span>(<span class='ident'>john</span>);
<span class='comment'>// `john` has already been deallocated</span>
}
<span class='kw'>fn</span> <span class='ident'>show</span>(<span class='ident'>person</span>: <span class='ident'>Person</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{} is {}"</span>, <span class='ident'>person</span>.<span class='ident'>name</span>, <span class='ident'>person</span>.<span class='ident'>age</span>);
<span class='comment'>// `john` will be deallocated here</span>
}
</pre>
<h1 id="move-1" class='section-header'><a
href="#move-1">Move</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Person</span> {
<span class='ident'>name</span>: <span class='ident'>String</span>,
<span class='ident'>age</span>: <span class='ident'>uint</span>
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='ident'>show</span>(<span class='ident'>john</span>);
<span class='ident'>show</span>(<span class='ident'>john</span>);
}
<span class='kw'>fn</span> <span class='ident'>show</span>(<span class='ident'>person</span>: <span class='ident'>Person</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{} is {}"</span>, <span class='ident'>person</span>.<span class='ident'>name</span>, <span class='ident'>person</span>.<span class='ident'>age</span>);
<span class='comment'>// `john` will be deallocated here</span>
}
</pre>
<h1 id="move-2" class='section-header'><a
href="#move-2">Move</a></h1>
<pre><code class="language-norust">error: use of moved value: `john`
show(john);
^
note: `john` moved here because it has type `Person`, which is non-copyable
show(john);
^
error: aborting due to previous error
</code></pre>
<h1 id="move-3" class='section-header'><a
href="#move-3">Move</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='ident'>String</span>, <span class='ident'>age</span>: <span class='ident'>uint</span> }
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='comment'>// `john` moves into show here</span>
<span class='ident'>show</span>(<span class='ident'>john</span>);
<span class='comment'>// `john` is deallocated by now</span>
<span class='comment'>// so the next line doesn't compile</span>
<span class='ident'>show</span>(<span class='ident'>john</span>);
}
<span class='kw'>fn</span> <span class='ident'>show</span>(<span class='ident'>person</span>: <span class='ident'>Person</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{} is {}"</span>, <span class='ident'>person</span>.<span class='ident'>name</span>, <span class='ident'>person</span>.<span class='ident'>age</span>);
<span class='comment'>// `john` will be deallocated here</span>
}
</pre>
<h1 id="move-(assignment)" class='section-header'><a
href="#move-(assignment)">Move (assignment)</a></h1>
<p>Assigning a value to a variable is similar to passing it to a function.</p>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='ident'>String</span>, <span class='ident'>age</span>: <span class='ident'>uint</span> }
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='comment'>// `john` moves into `john_bis` here</span>
<span class='kw'>let</span> <span class='ident'>john_bis</span> <span class='op'>=</span> <span class='ident'>john</span>;
<span class='comment'>// so the next line won't compile</span>
<span class='ident'>show</span>(<span class='ident'>john</span>);
}
<span class='kw'>fn</span> <span class='ident'>show</span>(<span class='ident'>person</span>: <span class='ident'>Person</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{} is {}"</span>, <span class='ident'>person</span>.<span class='ident'>name</span>, <span class='ident'>person</span>.<span class='ident'>age</span>);
<span class='comment'>// `john` will be deallocated here</span>
}
</pre>
<h1 id="ownership-&-borrowing" class='section-header'><a
href="#ownership-&-borrowing">Ownership & Borrowing</a></h1>
<p>When you create a resource, you're the <strong>owner</strong> of that resource.</p>
<p>Being an owner gives you some privileges:</p>
<ul>
<li>You control when that resource is deallocated.</li>
<li>You may lend that resource, <strong>immutably</strong>, to <strong>as many</strong> borrowers as you'd like.</li>
<li>You may lend that resource, <strong>mutably</strong>, to a <strong>single</strong> borrower.</li>
</ul>
<h1 id="ownership-&-borrowing-1" class='section-header'><a
href="#ownership-&-borrowing-1">Ownership & Borrowing</a></h1>
<p>But it also comes with some restrictions:</p>
<ul>
<li>If someone is borrowing your resource (either mutably or immutably), you may not mutate the resource or mutably lend it to someone.</li>
<li>If someone is mutably borrowing your resource, you may not lend it out at all (mutably or immutably) or access it in any way.</li>
</ul>
<h1 id="borrow" class='section-header'><a
href="#borrow">Borrow</a></h1>
<blockquote>
<p>You may lend that resource, <strong>immutably</strong>, to <strong>as many</strong> borrowers as you'd like.</p>
</blockquote>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='ident'>String</span>, <span class='ident'>age</span>: <span class='ident'>uint</span> }
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='comment'>// `john` moves into `show`.</span>
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>);
<span class='comment'>// `show` hands us `john` back.</span>
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>);
<span class='comment'>// the previous line will thus compile.</span>
}
<span class='kw'>fn</span> <span class='ident'>show</span>(<span class='ident'>person</span>: <span class='kw-2'>&</span><span class='ident'>Person</span>) {
<span class='macro'>println</span><span class='macro'>!</span>(<span class='string'>"{} is {}"</span>, <span class='ident'>person</span>.<span class='ident'>name</span>, <span class='ident'>person</span>.<span class='ident'>age</span>);
}
</pre>
<h1 id="borrow-1" class='section-header'><a
href="#borrow-1">Borrow</a></h1>
<pre><code class="language-norust">John is 42
John is 42
</code></pre>
<h1 id="mutable-borrow" class='section-header'><a
href="#mutable-borrow">Mutable borrow</a></h1>
<blockquote>
<p>You may lend that resource, <strong>mutably</strong>, to a <strong>single</strong> borrower.</p>
</blockquote>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='ident'>String</span>, <span class='ident'>age</span>: <span class='ident'>uint</span> }
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='ident'>grow_older</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>);
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>); <span class='comment'>// John is 43</span>
}
<span class='kw'>fn</span> <span class='ident'>grow_older</span>(<span class='ident'>person</span>: <span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>Person</span>) {
<span class='ident'>person</span>.<span class='ident'>age</span> <span class='op'>+=</span> <span class='number'>1</span>;
}
</pre>
<h1 id="mutable-borrow-1" class='section-header'><a
href="#mutable-borrow-1">Mutable borrow</a></h1>
<p>The following will compile too, as <code>grow_older</code> gives us <code>john</code> back:</p>
<pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>); <span class='comment'>// John is 42</span>
<span class='ident'>grow_older</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>);
<span class='ident'>grow_older</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>);
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>); <span class='comment'>// John is 44</span>
}
</pre>
<h1 id="mutable-borrow-2" class='section-header'><a
href="#mutable-borrow-2">Mutable borrow</a></h1>
<p>But this won't:</p>
<pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(), <span class='ident'>age</span>: <span class='number'>42</span> };
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>john_bis</span> <span class='op'>=</span> <span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>;
<span class='ident'>grow_older</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>);
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>);
}
</pre>
<h1 id="mutable-borrow-3" class='section-header'><a
href="#mutable-borrow-3">Mutable borrow</a></h1>
<pre><code class="language-norust">error: cannot borrow `john` as mutable more than once at a time
grow_older(&mut john);
^
note: previous borrow of `john` occurs here; the mutable borrow prevents subsequent moves, borrows, or modification of `p` until the borrow ends
let mut john_bis = &mut john;
^
note: previous borrow ends here
fn main() {
...
}
^
error: aborting due to previous error
</code></pre>
<h1 id="mutable-borrow-4" class='section-header'><a
href="#mutable-borrow-4">Mutable borrow</a></h1><pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>john</span> <span class='op'>=</span> <span class='ident'>Person</span> { <span class='ident'>name</span>: <span class='string'>"John"</span>.<span class='ident'>to_string</span>(),
<span class='ident'>age</span>: <span class='number'>42</span> };
<span class='comment'>// first borrow of `john` as mutable</span>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>john_bis</span> <span class='op'>=</span> <span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>;
<span class='comment'>// `john` cannot be mutably borrowed again, won't compile</span>
<span class='ident'>grow_older</span>(<span class='kw-2'>&</span><span class='kw-2'>mut</span> <span class='ident'>john</span>);
<span class='ident'>show</span>(<span class='kw-2'>&</span><span class='ident'>john</span>);
}
</pre>
<h1 id="lifetime-annotations" class='section-header'><a
href="#lifetime-annotations">Lifetime annotations</a></h1>
<p>Let's say we want to return a reference to a <code>struct</code>'s member field.</p>
<pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Point</span> { <span class='ident'>x</span>: <span class='ident'>f64</span>, <span class='ident'>y</span>: <span class='ident'>f64</span> }
<span class='kw'>fn</span> <span class='ident'>get_x</span>(<span class='ident'>point</span>: <span class='kw-2'>&</span><span class='ident'>Point</span>) <span class='op'>-></span> <span class='kw-2'>&</span><span class='ident'>f64</span> {
<span class='kw-2'>&</span><span class='ident'>point</span>.<span class='ident'>x</span>
}
</pre>
<h1 id="lifetime-annotations-1" class='section-header'><a
href="#lifetime-annotations-1">Lifetime annotations</a></h1>
<p>Until a few weeks ago, <code>get_x()</code> had to be written this way:</p>
<pre class='rust '>
<span class='kw'>fn</span> <span class='ident'>get_x</span><span class='op'><</span><span class='lifetime'>'a</span><span class='op'>></span>(<span class='ident'>point</span>: <span class='kw-2'>&</span><span class='lifetime'>'a</span> <span class='ident'>Point</span>) <span class='op'>-></span> <span class='kw-2'>&</span><span class='lifetime'>'a</span> <span class='ident'>f64</span> {
<span class='kw-2'>&</span><span class='ident'>point</span>.<span class='ident'>x</span>
}
</pre>
<p>But in that case, those annotations are now optional.</p>
<p><code>'a</code> represents the lifetime of <code>point</code>. We hereby specify that the reference we return must have the same lifetime as <code>point</code>.</p>
<h1 id="lifetime-annotations-2" class='section-header'><a
href="#lifetime-annotations-2">Lifetime annotations</a></h1>
<p>This means that code such as</p>
<pre class='rust '>
<span class='kw'>let</span> <span class='kw-2'>mut</span> <span class='ident'>p</span> <span class='op'>=</span> <span class='ident'>Point</span> { <span class='ident'>x</span>: <span class='number'>2.0</span>, <span class='ident'>y</span>: <span class='number'>3.0</span> };
<span class='kw'>let</span> <span class='ident'>x</span> <span class='op'>=</span> <span class='ident'>get_x</span>(<span class='kw-2'>&</span><span class='ident'>p</span>);
<span class='ident'>p</span> <span class='op'>=</span> <span class='ident'>Point</span> { <span class='ident'>x</span>: <span class='number'>4.0</span>, <span class='ident'>y</span>: <span class='number'>6.0</span> };
</pre>
<p>will not compile, because <code>x</code> outlives the value it is reference from.</p>
<h1 id="lifetime-annotations-3" class='section-header'><a
href="#lifetime-annotations-3">Lifetime annotations</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Line</span> { <span class='ident'>start</span>: <span class='kw-2'>&</span><span class='ident'>Point</span>, <span class='ident'>end</span>: <span class='kw-2'>&</span><span class='ident'>Point</span> }
<span class='kw'>fn</span> <span class='ident'>make_line</span>(<span class='ident'>p1</span>: <span class='kw-2'>&</span><span class='ident'>Point</span>, <span class='ident'>p2</span>: <span class='kw-2'>&</span><span class='ident'>Point</span>) <span class='op'>-></span> <span class='ident'>Line</span> {
<span class='ident'>Line</span> { <span class='ident'>start</span>: <span class='ident'>p1</span>, <span class='ident'>end</span>: <span class='ident'>p2</span> }
}
<span class='kw'>fn</span> <span class='ident'>main</span>() {
<span class='kw'>let</span> <span class='ident'>p1</span> <span class='op'>=</span> <span class='ident'>Point</span> { <span class='ident'>x</span>: <span class='number'>1.0</span>, <span class='ident'>y</span>: <span class='number'>2.3</span> };
<span class='kw'>let</span> <span class='ident'>p2</span> <span class='op'>=</span> <span class='ident'>Point</span> { <span class='ident'>x</span>: <span class='number'>2.0</span>, <span class='ident'>y</span>: <span class='number'>5.9</span> };
<span class='kw'>let</span> <span class='ident'>line</span> <span class='op'>=</span> <span class='ident'>make_line</span>(<span class='kw-2'>&</span><span class='ident'>p1</span>, <span class='kw-2'>&</span><span class='ident'>p2</span>);
}
</pre>
<blockquote>
<p>error: missing lifetime specifier</p>
</blockquote>
<h1 id="lifetime-annotations-4" class='section-header'><a
href="#lifetime-annotations-4">Lifetime annotations</a></h1><pre class='rust '>
<span class='kw'>struct</span> <span class='ident'>Line</span><span class='op'><</span><span class='lifetime'>'a</span><span class='op'>></span> {
<span class='ident'>start</span>: <span class='kw-2'>&</span><span class='lifetime'>'a</span> <span class='ident'>Point</span>,
<span class='ident'>end</span>: <span class='kw-2'>&</span><span class='lifetime'>'a</span> <span class='ident'>Point</span>
}
<span class='kw'>fn</span> <span class='ident'>make_line</span><span class='op'><</span><span class='lifetime'>'b</span><span class='op'>></span>(<span class='ident'>p1</span>: <span class='kw-2'>&</span><span class='lifetime'>'b</span> <span class='ident'>Point</span>, <span class='ident'>p2</span>: <span class='kw-2'>&</span><span class='lifetime'>'b</span> <span class='ident'>Point</span>) <span class='op'>-></span> <span class='ident'>Box</span><span class='op'><</span><span class='ident'>Line</span><span class='op'><</span><span class='lifetime'>'b</span><span class='op'>>></span> {