Skip to content

Commit 9bd995a

Browse files
committed
rustfmt all rust code snippets
1 parent d409625 commit 9bd995a

File tree

9 files changed

+338
-322
lines changed

9 files changed

+338
-322
lines changed

lectures/Rust/src/basics.md

Lines changed: 77 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ Variables in Rust are declared with the `let` keyword (thanks FP).
3131

3232
```rust,editable
3333
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+
);
3639
}
3740
```
3841

@@ -47,9 +50,15 @@ Let's look at what happens when we try to change the value of `answer`.
4750
```rust,editable
4851
fn main() {
4952
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+
);
5157
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+
);
5362
}
5463
```
5564

@@ -60,10 +69,15 @@ The following works fine.
6069
```rust,editable
6170
fn main() {
6271
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+
);
6476
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+
);
6781
}
6882
```
6983

@@ -111,7 +125,7 @@ Snippet from the Rust book:
111125
```rust, editable
112126
fn main() {
113127
let c = 'z';
114-
let z : char = 'ℤ'; // with explicit type annotation
128+
let z: char = 'ℤ'; // with explicit type annotation
115129
let heart_eyed_cat = '😻';
116130
}
117131
```
@@ -123,18 +137,18 @@ fn main() {
123137

124138
```rust,editable
125139
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');
128142
129-
println!("My tuple is {:?}", t);
143+
println!("My tuple is {:?}", t);
130144
131-
// eliminating tuples: projection
132-
println!("The first component is {}", t.0);
145+
// eliminating tuples: projection
146+
println!("The first component is {}", t.0);
133147
134-
// eliminating tuples: decomposition pattern
135-
let (x, y, z) = t;
148+
// eliminating tuples: decomposition pattern
149+
let (x, y, z) = t;
136150
137-
println!("The first component is {}", x);
151+
println!("The first component is {}", x);
138152
}
139153
```
140154

@@ -151,9 +165,9 @@ type is fixed and statically known. They are allocated on the stack.
151165

152166
```rust,editable
153167
fn main() {
154-
let a : [u32;5]= [1,2,3,4,5];
168+
let a: [u32; 5] = [1, 2, 3, 4, 5];
155169
156-
println!("The 2nd element is {}", a[1]);
170+
println!("The 2nd element is {}", a[1]);
157171
}
158172
```
159173

@@ -167,14 +181,14 @@ The following program compiles fine and throws an error at runtime. You can also
167181
see an example of Rust function (yes, arrows again).
168182

169183
```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];
172186
}
173187
174188
fn main() {
175-
let a : [u32;5]= [1,2,3,4,5];
189+
let a: [u32; 5] = [1, 2, 3, 4, 5];
176190
177-
println!("The 2nd element is {}", access(a, 7));
191+
println!("The 2nd element is {}", access(a, 7));
178192
}
179193
```
180194

@@ -190,42 +204,40 @@ Here are some examples of Rust functions.
190204

191205

192206
```rust,editable
193-
fn fourtytwo() -> u32{
194-
return 42;
207+
fn fourtytwo() -> u32 {
208+
return 42;
195209
}
196210
197211
fn sayhi() {
198-
println!("Hello!");
212+
println!("Hello!");
199213
}
200214
201-
fn add(x: u32, y:u32) -> u32{
202-
x+y
215+
fn add(x: u32, y: u32) -> u32 {
216+
x + y
203217
}
204218
205219
fn main() {
206-
println!("One more time {}", fourtytwo());
207-
208-
sayhi();
220+
println!("One more time {}", fourtytwo());
209221
210-
println!("Add two numbers: {}", add(5,6));
222+
sayhi();
211223
224+
println!("Add two numbers: {}", add(5, 6));
212225
}
213226
```
214227

215228
We can declare function parameters as mutable.
216229

217230
```rust,editable
218-
fn add1(mut t : (u32, u32)) -> u32 {
231+
fn add1(mut t: (u32, u32)) -> u32 {
219232
t.0 += 1;
220233
return t.0;
221234
}
222235
223236
fn main() {
224-
let mut t = (1,2);
237+
let mut t = (1, 2);
225238
226239
println!("add1 result: {}", add1(t));
227240
println!("The first component is {}", t.0);
228-
229241
}
230242
```
231243

@@ -237,66 +249,67 @@ Book](https://doc.rust-lang.org/book/ch03-05-control-flow.html).
237249

238250

239251
```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+
}
245260
}
246261
247-
248-
fn fib_iter1(n : u32) -> u32 {
262+
fn fib_iter1(n: u32) -> u32 {
249263
let mut i = n;
250264
let mut curr = 0;
251265
let mut next = 1;
252266
253267
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+
}
259275
}
260276
}
261277
262-
fn fib_iter2(n : u32) -> u32 {
278+
fn fib_iter2(n: u32) -> u32 {
263279
let mut i = n;
264280
let mut curr = 0;
265281
let mut next = 1;
266282
267283
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;
272288
}
273289
274290
return curr;
275291
}
276292
277-
fn fib_iter3(n : u32) -> u32 {
293+
fn fib_iter3(n: u32) -> u32 {
278294
let mut i = n;
279295
let mut curr = 0;
280296
let mut next = 1;
281297
282298
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;
286302
}
287303
288304
return curr;
289305
}
290306
291-
292307
fn main() {
308+
let n = 8;
293309
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));
301314
}
302-
```
315+
```

lectures/Rust/src/generics.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ functionality that is common to all types.
99
A very simple example of a polymorphic function is the identity function.
1010

1111
```rust,editable
12-
fn id<T>(x:T) -> T {
12+
fn id<T>(x: T) -> T {
1313
x
1414
}
1515
@@ -26,12 +26,12 @@ Function can be generic over more than one types. This is illustrated in the
2626
following example.
2727

2828
```rust,editable
29-
fn make_tuple<T,R>(x:T, y:R) -> (T,R) {
29+
fn make_tuple<T, R>(x: T, y: R) -> (T, R) {
3030
(x, y)
3131
}
3232
3333
fn main() {
34-
println!("{:?}", make_tuple(42,11));
34+
println!("{:?}", make_tuple(42, 11));
3535
}
3636
```
3737

@@ -46,10 +46,10 @@ The example also illustrates macros in Rust.
4646
#[derive(Debug)]
4747
enum List<T> {
4848
Nil,
49-
Cons(T, Box<List<T>>)
49+
Cons(T, Box<List<T>>),
5050
}
5151
52-
use List::{Cons,Nil}; // To be able to use Cons and Nil without qualifying them.
52+
use List::{Cons, Nil}; // To be able to use Cons and Nil without qualifying them.
5353
5454
// Define some macros for lists
5555
macro_rules! list {
@@ -69,14 +69,12 @@ macro_rules! list {
6969
};
7070
}
7171
72-
7372
impl<T> List<T> {
74-
7573
// return a reference to the head of the list (if any) or nothing
7674
fn head(&self) -> Option<&T> {
7775
match self {
7876
Nil => None,
79-
Cons(x,_) => Some(x)
77+
Cons(x, _) => Some(x),
8078
}
8179
}
8280
@@ -87,7 +85,7 @@ impl<T> List<T> {
8785
8886
while let Cons(value, mut next) = current {
8987
current = *next; // Move to the next node
90-
*next = rev ; // Reverse the link
88+
*next = rev; // Reverse the link
9189
rev = Cons(value, next); // Update the reversed list
9290
}
9391
@@ -101,17 +99,17 @@ impl<T> List<T> {
10199
fn length(&self) -> u64 {
102100
match self {
103101
Nil => 0,
104-
Cons(_,l) => 1 + l.length()
102+
Cons(_, l) => 1 + l.length(),
105103
}
106104
}
107105
}
108106
109107
fn main() {
110-
let mut l = list![1,2,3];
108+
let mut l = list![1, 2, 3];
111109
112110
l.rev();
113111
114-
println!{"{:?}",l}
112+
println!("{:?}",l);
115113
}
116114
```
117115

0 commit comments

Comments
 (0)