-
Notifications
You must be signed in to change notification settings - Fork 0
/
jopenspace-rust-talk.html
187 lines (165 loc) · 4.24 KB
/
jopenspace-rust-talk.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
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Lato);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Lato'; }
h1, h2, h3 {
font-family: 'Lato';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# Rust
Jarda Šnajdr @jsnajdr
---
### Rust: safe systems programming language
- invented at Mozilla
- memory-safe, no undefined behavior, no segfaults
- thread-safe, no data races
- systems language - full control over hardware
- including memory - no garbage collector
- target audience: frustrated C/C++ developers
- Servo: safe, fast and parallel browser engine
---
First taste of Rust code:
```rust
fn gcd(mut n: u64, mut m: u64) -> u64 {
assert!(n != 0 && m != 0);
while m != 0 {
if m < n {
let t = m; m = n; n = t;
}
m = m % n;
}
n
}
```
---
Generic functions with traits:
```rust
fn min<T: Ord>(a: T, b: T) -> T {
if a <= b { a } else { b }
}
```
---
Enumerated (algebraic) types:
```rust
enum Option<T> {
None,
Some(T)
}
#[must_use]
enum Result<T, E> {
Ok(T),
Err(E)
}
```
---
Function that returns a number maybe:
```rust
fn safe_div(n: i32, d: i32) -> Option<i32> {
if d == 0 {
return None;
}
return Some(n / d);
}
```
---
Working with the result with pattern matching:
```rust
match safe_div(10, 0) {
None => println!("No quotient."),
Some(u) => println!("quotient is {}", u)
}
if let Some(v) = safe_div(10, 5) {
println!("quotient is {}", v);
}
```
- impossible to dereference a null pointer!
- safe error handling (no more rv == -1)
---
Every value has a single owner at any time:
```rust
let x = "Hello".to_owned();
let y = x; // move
println!("x is {}", x);
println!("y is {}", y); // error
```
---
Borrow a reference:
```rust
let x = "Hello".to_owned();
let y = &x;
println!("x is {}", x);
println!("y is {}", y); // ok
```
---
Reference must not outlive the owner:
```rust
let borrow;
{
let x = "Hello".to_owned();
borrow = &x;
}
```
---
Can modify value only when exclusively owned:
```rust
let mut x = "Hello".to_owned();
let y = &x;
x = "World".to_owned(); // error
```
---
Threading with races:
```rust
fn concurrent_inc() {
let mut x = 1;
let thread1 = std::thread::spawn(|| { x += 8 });
let thread2 = std::thread::spawn(|| { x += 27 });
}
```
---
Threading with mutexes and scoped threads:
```rust
let x = std::sync::Mutex::new(1);
{
let thread1 = std::thread::scoped(|| {
let mut data = x.lock().unwrap();
*data += 8;
});
let thread2 = std::thread::scoped(|| {
let mut data = x.lock().unwrap();
*data += 27;
});
}
assert_eq!(*x.lock().unwrap(), 36);
```
---
More about threads:
- sharing with reference counting (`Rc`, `Arc`)
- sending messages with channels
- types have `Send` and `Sync` traits
---
Packaging with Cargo
- similar to NPM, designed by Yehuda Katz (RoR, Ember)
- package repository at crates.io
---
Learn more:
- https://rust-lang.org
- https://github.com/servo/servo
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script>
var slideshow = remark.create();
</script>
</body>
</html>