Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format all rust code snippets with rustfmt #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 77 additions & 64 deletions lectures/Rust/src/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ Variables in Rust are declared with the `let` keyword (thanks FP).

```rust,editable
fn main() {
let answer : u64 = 42;
println!("The answer to life the universe and everything is {}.", answer);
let answer: u64 = 42;
println!(
"The answer to life the universe and everything is {}.",
answer
);
}
```

Expand All @@ -47,9 +50,15 @@ Let's look at what happens when we try to change the value of `answer`.
```rust,editable
fn main() {
let answer = 42;
println!("The answer to life the universe and everything is {}.", answer);
println!(
"The answer to life the universe and everything is {}.",
answer
);
answer = 41;
println!("I changed my mind. The answer to life the universe and everything is {}.", answer);
println!(
"I changed my mind. The answer to life the universe and everything is {}.",
answer
);
}
```

Expand All @@ -60,10 +69,15 @@ The following works fine.
```rust,editable
fn main() {
let mut answer = 42;
println!("The answer to life the universe and everything is {}.", answer);
println!(
"The answer to life the universe and everything is {}.",
answer
);
answer = 41;
println!("I changed my mind. The answer to life the universe and everything is {}.", answer);

println!(
"I changed my mind. The answer to life the universe and everything is {}.",
answer
);
}
```

Expand Down Expand Up @@ -111,7 +125,7 @@ Snippet from the Rust book:
```rust, editable
fn main() {
let c = 'z';
let z : char = 'ℤ'; // with explicit type annotation
let z: char = 'ℤ'; // with explicit type annotation
let heart_eyed_cat = '😻';
}
```
Expand All @@ -123,18 +137,18 @@ fn main() {

```rust,editable
fn main() {
// constructing tuples
let t : (u64, f32, char) = (42, 10.0, 'z');
// constructing tuples
let t: (u64, f32, char) = (42, 10.0, 'z');

println!("My tuple is {:?}", t);
println!("My tuple is {:?}", t);

// eliminating tuples: projection
println!("The first component is {}", t.0);
// eliminating tuples: projection
println!("The first component is {}", t.0);

// eliminating tuples: decomposition pattern
let (x, y, z) = t;
// eliminating tuples: decomposition pattern
let (x, y, z) = t;

println!("The first component is {}", x);
println!("The first component is {}", x);
}
```

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

```rust,editable
fn main() {
let a : [u32;5]= [1,2,3,4,5];
let a: [u32; 5] = [1, 2, 3, 4, 5];

println!("The 2nd element is {}", a[1]);
println!("The 2nd element is {}", a[1]);
}
```

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

```rust,editable
fn access(a : [u32;5], n : usize) -> u32{
return a[n];
fn access(a: [u32; 5], n: usize) -> u32 {
return a[n];
}

fn main() {
let a : [u32;5]= [1,2,3,4,5];
let a: [u32; 5] = [1, 2, 3, 4, 5];

println!("The 2nd element is {}", access(a, 7));
println!("The 2nd element is {}", access(a, 7));
}
```

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


```rust,editable
fn fourtytwo() -> u32{
return 42;
fn fourtytwo() -> u32 {
return 42;
}

fn sayhi() {
println!("Hello!");
println!("Hello!");
}

fn add(x: u32, y:u32) -> u32{
x+y
fn add(x: u32, y: u32) -> u32 {
x + y
}

fn main() {
println!("One more time {}", fourtytwo());

sayhi();
println!("One more time {}", fourtytwo());

println!("Add two numbers: {}", add(5,6));
sayhi();

println!("Add two numbers: {}", add(5, 6));
}
```

We can declare function parameters as mutable.

```rust,editable
fn add1(mut t : (u32, u32)) -> u32 {
fn add1(mut t: (u32, u32)) -> u32 {
t.0 += 1;
return t.0;
}

fn main() {
let mut t = (1,2);
let mut t = (1, 2);

println!("add1 result: {}", add1(t));
println!("The first component is {}", t.0);

}
```

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


```rust,editable

fn fib(n : u32) -> u32 {
if n == 0 { 0 }
else if n == 1 { 1 }
else { fib(n-1) + fib(n-2) }
fn fib(n: u32) -> u32 {
if n == 0 {
0
} else if n == 1 {
1
} else {
fib(n - 1) + fib(n - 2)
}
}


fn fib_iter1(n : u32) -> u32 {
fn fib_iter1(n: u32) -> u32 {
let mut i = n;
let mut curr = 0;
let mut next = 1;

loop {
let tmp = curr;
curr = next;
next = next + tmp;
i-=1;
if i == 0 { break curr }
let tmp = curr;
curr = next;
next = next + tmp;
i -= 1;
if i == 0 {
break curr;
}
}
}

fn fib_iter2(n : u32) -> u32 {
fn fib_iter2(n: u32) -> u32 {
let mut i = n;
let mut curr = 0;
let mut next = 1;

while i != 0 {
let tmp = curr;
curr = next;
next = next + tmp;
i-=1;
let tmp = curr;
curr = next;
next = next + tmp;
i -= 1;
}

return curr;
}

fn fib_iter3(n : u32) -> u32 {
fn fib_iter3(n: u32) -> u32 {
let mut i = n;
let mut curr = 0;
let mut next = 1;

for i in 0..n {
let tmp = curr;
curr = next;
next = next + tmp;
let tmp = curr;
curr = next;
next = next + tmp;
}

return curr;
}


fn main() {
let n = 8;

let n = 8;

println!("Functional: The {}th fib is: {}", n, fib(n));
println!("Iterative 1: The {}th fib is: {}", n, fib_iter1(n));
println!("Iterative 2: The {}th fib is: {}", n, fib_iter2(n));
println!("Iterative 3: The {}th fib is: {}", n, fib_iter3(n));

println!("Functional: The {}th fib is: {}", n, fib(n));
println!("Iterative 1: The {}th fib is: {}", n, fib_iter1(n));
println!("Iterative 2: The {}th fib is: {}", n, fib_iter2(n));
println!("Iterative 3: The {}th fib is: {}", n, fib_iter3(n));
}
```
```
22 changes: 10 additions & 12 deletions lectures/Rust/src/generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ functionality that is common to all types.
A very simple example of a polymorphic function is the identity function.

```rust,editable
fn id<T>(x:T) -> T {
fn id<T>(x: T) -> T {
x
}

Expand All @@ -26,12 +26,12 @@ Function can be generic over more than one types. This is illustrated in the
following example.

```rust,editable
fn make_tuple<T,R>(x:T, y:R) -> (T,R) {
fn make_tuple<T, R>(x: T, y: R) -> (T, R) {
(x, y)
}

fn main() {
println!("{:?}", make_tuple(42,11));
println!("{:?}", make_tuple(42, 11));
}
```

Expand All @@ -46,10 +46,10 @@ The example also illustrates macros in Rust.
#[derive(Debug)]
enum List<T> {
Nil,
Cons(T, Box<List<T>>)
Cons(T, Box<List<T>>),
}

use List::{Cons,Nil}; // To be able to use Cons and Nil without qualifying them.
use List::{Cons, Nil}; // To be able to use Cons and Nil without qualifying them.

// Define some macros for lists
macro_rules! list {
Expand All @@ -69,14 +69,12 @@ macro_rules! list {
};
}


impl<T> List<T> {

// return a reference to the head of the list (if any) or nothing
fn head(&self) -> Option<&T> {
match self {
Nil => None,
Cons(x,_) => Some(x)
Cons(x, _) => Some(x),
}
}

Expand All @@ -87,7 +85,7 @@ impl<T> List<T> {

while let Cons(value, mut next) = current {
current = *next; // Move to the next node
*next = rev ; // Reverse the link
*next = rev; // Reverse the link
rev = Cons(value, next); // Update the reversed list
}

Expand All @@ -101,17 +99,17 @@ impl<T> List<T> {
fn length(&self) -> u64 {
match self {
Nil => 0,
Cons(_,l) => 1 + l.length()
Cons(_, l) => 1 + l.length(),
}
}
}

fn main() {
let mut l = list![1,2,3];
let mut l = list![1, 2, 3];

l.rev();

println!{"{:?}",l}
println!("{:?}",l);
}
```

Expand Down
Loading