-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
394 lines (346 loc) · 9.88 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Coding Students Meetup - Rust Overview</title>
<link rel="stylesheet" href="revealjs/css/reset.css">
<link rel="stylesheet" href="revealjs/css/reveal.css">
<link rel="stylesheet" href="revealjs/css/theme/black.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="revealjs/lib/css/monokai.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) ? 'revealjs/css/print/pdf.css' : 'revealjs/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Rust Overview</h1>
<h3>Sven Kube</h3>
</section>
<section>
<h2>Table of Contents</h2>
<ul>
<li>Rust Compiler</li>
<li>Ownership</li>
<li>Pattern Matching</li>
<li>Error Handling</li>
<li>Traits</li>
</ul>
</section>
<section><!-- Rust compiler saves you -->
<section>
<h1>Rust Compiler</h1>
</section>
<section>
<h3>Variables and Mutability</h3>
<pre><code class="hljs rust" data-trim data-line-numbers="">
fn main() {
let x = 42;
x = 773;
println!("{}", x);
}
</code></pre>
<pre><code class="hljs rust" data-trim data-line-numbers="">
error[E0384]: cannot assign twice to immutable variable `x`
--> test.rs:5:5
|
3 | let x = 42;
| - first assignment to `x`
4 |
5 | x = 773;
| ^^^^^^^ cannot assign twice to immutable variable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0384`.
</code></pre>
</section>
<section>
<h3>Variables and Mutability</h3>
<pre><code class="hljs rust" data-trim data-line-numbers="">
fn main() {
let mut x = 42;
x = 773;
println!("{}", x);
}
</code></pre>
<pre><code class="hljs rust" data-trim data-line-numbers="">
warning: value assigned to `x` is never read
--> test.rs:2:13
|
2 | let mut x = 42;
| ^
|
= note: #[warn(unused_assignments)] on by default
= help: maybe it is overwritten before being read?
</code></pre>
</section>
<section>
<p>In C++:</p>
<pre><code class="hljs cpp" data-trim data-line-numbers>
int* bad_ptr(){
int some_int = 42;
return &some_int;
}
int main(){
int* some_ptr = bad_ptr();
std::cout << *some_ptr << std::endl;
}
</code></pre>
<p class="fragment">
Compiles with a warning
</p>
<p>
<pre><code class="hljs fragment">
“./return-local-addr” terminated by signal SIGSEGV (Address boundary error)
</code></pre>
</p>
</section>
<section>
<p>In Rust:</p>
<pre><code class="hljs rust" data-trim data-line-numbers="1-4,8">
fn bad_ptr<'a>(x: i32) -> &'a i32 {
let y : &i32 = &x;
y
}
fn main() {
let a = 42;
let b : i32 = *bad_ptr(a);
println!("a: {}, b: {}", a, b);
}
</code></pre>
</section>
<section>
<p>Rust-Compiler says:</p>
<pre><code class="hljs" data-trim>
error[E0597]: `x` does not live long enough
--> return-local-addr.rs:2:21
|
2 | let y : &i32 = &x;
| ^ borrowed value does not live long enough
3 | y
4 | }
| - borrowed value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 1:12...
--> return-local-addr.rs:1:12
|
1 | fn bad_ptr<'a>(x: i32) -> &'a i32 {
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0597`.
</code></pre>
</section>
</section>
<section><!-- Ownership -->
<section>
<h1>Ownership</h1>
</section>
<section>
<h3>Ownership Rules</h3>
<ul>
<li>Each value in Rust has a variable that’s called its owner.</li>
<li>There can only be one owner at a time.</li>
<li>When the owner goes out of scope, the value will be dropped.</li>
</ul>
</section>
<section>
<h3>References and Borrowing</h3>
<pre><code class="hljs rust" data-trim data-line-numbers>
fn main() {
let s1 = String::from("Coding Students");
let len = calculate_length(&s1);
println!("The length of '{}' is {}.", s1, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
}
</code></pre>
</section>
<section>
<h3>References and Borrowing</h3>
<pre><code class="hljs rust" data-trim data-line-numbers>
fn main() {
let mut s = String::from("hello");
change(&mut s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}
</code></pre>
</section>
<section>
<h3>The Rules of References</h3>
<ul>
<li>
At any given time, you can have either one mutable reference or any number of immutable references.
</li><li>
References must always be valid.
</li>
</ul>
</section>
</section>
<section><!-- Pattern Matching -->
<section>
<h1>Pattern Matching</h1>
</section>
<section data-transition="fade">
<h2>Patterns</h2>
<pre><code class="hljs rust" data-trim data-line-numbers="3-8">
let x = 1;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
_ => println!("anything"),
}
</code></pre>
</section>
<section data-transition="fade">
<h2>Multiple patterns</h2>
<pre><code class="hljs rust" data-trim data-line-numbers="4">
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
</code></pre>
</section>
<section data-transition="fade">
<h2>Destructuring</h2>
<pre><code class="hljs rust" data-trim data-line-numbers="1-4,9">
struct Point {
x: i32,
y: i32,
}
let some_point = Point { x: 0, y: 1 };
match some_point {
Point { x, y } => println!("({}, {})", x, y),
}
</code></pre>
</section>
</section>
<section><!-- Errors -->
<section>
<h1>Error Handling</h1>
</section>
<section>
<h3>Reading a file</h3>
</section>
<section>
<p>With Type-Inference:</p>
<pre><code>
use std::fs::File;
fn main() {
let f = File::open("hello.txt");
}
</code></pre>
</section>
<section>
<p>Without:</p>
<pre><code class="hljs rust" data-trim data-line-numbers>
use std::fs::File;
fn main() {
let f: std::result::Result<std::fs::File, std::io::Error> = File::open("hello.txt");
}
</code></pre>
</section>
<section>
<pre><code class="hljs rust" data-trim data-line-numbers>
let f = File::open("hello.txt");
let f = match f {
Ok(file) => file,
Err(error) => {
panic!("Problem opening the file: {:?}", error)
},
};
</code></pre>
</section>
<section>
<pre><code class="hljs rust" data-trim data-line-numbers="1,3">
let f = File::open("hello.txt").unwrap();
let f = File::open("hello.txt").expect("Failed to open hello.txt");
</code></pre>
</section>
</section>
<section><!-- Traits -->
<section>
<h1>Traits</h1>
</section>
<section>
<pre><code class="hljs rust" data-trim data-line-numbers>
struct Point {
x: f64,
y: f64,
}
fn point_to_string(point: &Point) -> String { ... }
impl Point {
fn to_string(&self) -> String { ... }
}
</code></pre>
</section>
<section>
<h2>The Hash Trait</h2>
<pre class="fragment"><code class="hljs rust" data-trim data-line-numbers="">
trait Hash {
fn hash(&self) -> u64;
}
</code></pre>
<pre class="fragment"><code class="hljs rust" data-trim data-line-numbers>
impl Hash for bool {
fn hash(&self) -> u64 {
if *self { 0 } else { 1 }
}
}
impl Hash for i64 {
fn hash(&self) -> u64 {
*self as u64
}
}
</code></pre>
</section>
</section>
<section>
<a href="https://doc.rust-lang.org/book/">Rust Book</a>
</section>
<section>
<h2>There is more:</h2>
<ul>
<li>rustfmt</li>
<li>Cargo</li>
<li>Clippy</li>
<li>rocket</li>
<li>serde</li>
<li>diesel</li>
<li>...</li>
</ul>
</section>
<section>
<h2>Slides:</h2>
<a href="https://github.com/SvenKube/Coding-Students-Meetup-Rust-Overview">https://github.com/SvenKube/Coding-Students-Meetup-Rust-Overview</a>
</section>
</div>
</div>
<script src="revealjs/js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
dependencies: [
{ src: 'revealjs/plugin/markdown/marked.js' },
{ src: 'revealjs/plugin/markdown/markdown.js' },
{ src: 'revealjs/plugin/notes/notes.js', async: true },
{ src: 'revealjs/plugin/highlight/highlight.js', async: true }
]
});
</script>
</body>
</html>