@@ -31,8 +31,11 @@ Variables in Rust are declared with the `let` keyword (thanks FP).
31
31
32
32
``` rust,editable
33
33
fn main() {
34
- let answer : u64 = 42;
35
- println!("The answer to life the universe and everything is {}.", answer);
34
+ let answer: u64 = 42;
35
+ println!(
36
+ "The answer to life the universe and everything is {}.",
37
+ answer
38
+ );
36
39
}
37
40
```
38
41
@@ -47,9 +50,15 @@ Let's look at what happens when we try to change the value of `answer`.
47
50
``` rust,editable
48
51
fn main() {
49
52
let answer = 42;
50
- println!("The answer to life the universe and everything is {}.", answer);
53
+ println!(
54
+ "The answer to life the universe and everything is {}.",
55
+ answer
56
+ );
51
57
answer = 41;
52
- println!("I changed my mind. The answer to life the universe and everything is {}.", answer);
58
+ println!(
59
+ "I changed my mind. The answer to life the universe and everything is {}.",
60
+ answer
61
+ );
53
62
}
54
63
```
55
64
@@ -60,10 +69,15 @@ The following works fine.
60
69
``` rust,editable
61
70
fn main() {
62
71
let mut answer = 42;
63
- println!("The answer to life the universe and everything is {}.", answer);
72
+ println!(
73
+ "The answer to life the universe and everything is {}.",
74
+ answer
75
+ );
64
76
answer = 41;
65
- println!("I changed my mind. The answer to life the universe and everything is {}.", answer);
66
-
77
+ println!(
78
+ "I changed my mind. The answer to life the universe and everything is {}.",
79
+ answer
80
+ );
67
81
}
68
82
```
69
83
@@ -111,7 +125,7 @@ Snippet from the Rust book:
111
125
``` rust, editable
112
126
fn main() {
113
127
let c = 'z';
114
- let z : char = 'ℤ'; // with explicit type annotation
128
+ let z: char = 'ℤ'; // with explicit type annotation
115
129
let heart_eyed_cat = '😻';
116
130
}
117
131
```
@@ -123,18 +137,18 @@ fn main() {
123
137
124
138
``` rust,editable
125
139
fn main() {
126
- // constructing tuples
127
- let t : (u64, f32, char) = (42, 10.0, 'z');
140
+ // constructing tuples
141
+ let t: (u64, f32, char) = (42, 10.0, 'z');
128
142
129
- println!("My tuple is {:?}", t);
143
+ println!("My tuple is {:?}", t);
130
144
131
- // eliminating tuples: projection
132
- println!("The first component is {}", t.0);
145
+ // eliminating tuples: projection
146
+ println!("The first component is {}", t.0);
133
147
134
- // eliminating tuples: decomposition pattern
135
- let (x, y, z) = t;
148
+ // eliminating tuples: decomposition pattern
149
+ let (x, y, z) = t;
136
150
137
- println!("The first component is {}", x);
151
+ println!("The first component is {}", x);
138
152
}
139
153
```
140
154
@@ -151,9 +165,9 @@ type is fixed and statically known. They are allocated on the stack.
151
165
152
166
``` rust,editable
153
167
fn main() {
154
- let a : [u32;5] = [1,2,3,4, 5];
168
+ let a: [u32; 5] = [1, 2, 3, 4, 5];
155
169
156
- println!("The 2nd element is {}", a[1]);
170
+ println!("The 2nd element is {}", a[1]);
157
171
}
158
172
```
159
173
@@ -167,14 +181,14 @@ The following program compiles fine and throws an error at runtime. You can also
167
181
see an example of Rust function (yes, arrows again).
168
182
169
183
``` rust,editable
170
- fn access(a : [u32;5], n : usize) -> u32{
171
- return a[n];
184
+ fn access(a: [u32; 5], n: usize) -> u32 {
185
+ return a[n];
172
186
}
173
187
174
188
fn main() {
175
- let a : [u32;5] = [1,2,3,4, 5];
189
+ let a: [u32; 5] = [1, 2, 3, 4, 5];
176
190
177
- println!("The 2nd element is {}", access(a, 7));
191
+ println!("The 2nd element is {}", access(a, 7));
178
192
}
179
193
```
180
194
@@ -190,42 +204,40 @@ Here are some examples of Rust functions.
190
204
191
205
192
206
``` rust,editable
193
- fn fourtytwo() -> u32{
194
- return 42;
207
+ fn fourtytwo() -> u32 {
208
+ return 42;
195
209
}
196
210
197
211
fn sayhi() {
198
- println!("Hello!");
212
+ println!("Hello!");
199
213
}
200
214
201
- fn add(x: u32, y:u32) -> u32{
202
- x+ y
215
+ fn add(x: u32, y: u32) -> u32 {
216
+ x + y
203
217
}
204
218
205
219
fn main() {
206
- println!("One more time {}", fourtytwo());
207
-
208
- sayhi();
220
+ println!("One more time {}", fourtytwo());
209
221
210
- println!("Add two numbers: {}", add(5,6) );
222
+ sayhi( );
211
223
224
+ println!("Add two numbers: {}", add(5, 6));
212
225
}
213
226
```
214
227
215
228
We can declare function parameters as mutable.
216
229
217
230
``` rust,editable
218
- fn add1(mut t : (u32, u32)) -> u32 {
231
+ fn add1(mut t: (u32, u32)) -> u32 {
219
232
t.0 += 1;
220
233
return t.0;
221
234
}
222
235
223
236
fn main() {
224
- let mut t = (1,2);
237
+ let mut t = (1, 2);
225
238
226
239
println!("add1 result: {}", add1(t));
227
240
println!("The first component is {}", t.0);
228
-
229
241
}
230
242
```
231
243
@@ -237,66 +249,67 @@ Book](https://doc.rust-lang.org/book/ch03-05-control-flow.html).
237
249
238
250
239
251
``` rust,editable
240
-
241
- fn fib(n : u32) -> u32 {
242
- if n == 0 { 0 }
243
- else if n == 1 { 1 }
244
- else { fib(n-1) + fib(n-2) }
252
+ fn fib(n: u32) -> u32 {
253
+ if n == 0 {
254
+ 0
255
+ } else if n == 1 {
256
+ 1
257
+ } else {
258
+ fib(n - 1) + fib(n - 2)
259
+ }
245
260
}
246
261
247
-
248
- fn fib_iter1(n : u32) -> u32 {
262
+ fn fib_iter1(n: u32) -> u32 {
249
263
let mut i = n;
250
264
let mut curr = 0;
251
265
let mut next = 1;
252
266
253
267
loop {
254
- let tmp = curr;
255
- curr = next;
256
- next = next + tmp;
257
- i-=1;
258
- if i == 0 { break curr }
268
+ let tmp = curr;
269
+ curr = next;
270
+ next = next + tmp;
271
+ i -= 1;
272
+ if i == 0 {
273
+ break curr;
274
+ }
259
275
}
260
276
}
261
277
262
- fn fib_iter2(n : u32) -> u32 {
278
+ fn fib_iter2(n: u32) -> u32 {
263
279
let mut i = n;
264
280
let mut curr = 0;
265
281
let mut next = 1;
266
282
267
283
while i != 0 {
268
- let tmp = curr;
269
- curr = next;
270
- next = next + tmp;
271
- i-= 1;
284
+ let tmp = curr;
285
+ curr = next;
286
+ next = next + tmp;
287
+ i -= 1;
272
288
}
273
289
274
290
return curr;
275
291
}
276
292
277
- fn fib_iter3(n : u32) -> u32 {
293
+ fn fib_iter3(n: u32) -> u32 {
278
294
let mut i = n;
279
295
let mut curr = 0;
280
296
let mut next = 1;
281
297
282
298
for i in 0..n {
283
- let tmp = curr;
284
- curr = next;
285
- next = next + tmp;
299
+ let tmp = curr;
300
+ curr = next;
301
+ next = next + tmp;
286
302
}
287
303
288
304
return curr;
289
305
}
290
306
291
-
292
307
fn main() {
308
+ let n = 8;
293
309
294
- let n = 8;
295
-
296
- println!("Functional: The {}th fib is: {}", n, fib(n));
297
- println!("Iterative 1: The {}th fib is: {}", n, fib_iter1(n));
298
- println!("Iterative 2: The {}th fib is: {}", n, fib_iter2(n));
299
- println!("Iterative 3: The {}th fib is: {}", n, fib_iter3(n));
300
-
310
+ println!("Functional: The {}th fib is: {}", n, fib(n));
311
+ println!("Iterative 1: The {}th fib is: {}", n, fib_iter1(n));
312
+ println!("Iterative 2: The {}th fib is: {}", n, fib_iter2(n));
313
+ println!("Iterative 3: The {}th fib is: {}", n, fib_iter3(n));
301
314
}
302
- ```
315
+ ```
0 commit comments