Skip to content

Commit 4886402

Browse files
committed
archive previous course edition
1 parent 8262649 commit 4886402

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+3045
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
+++
2+
title = "Oragnizational lesson"
3+
date = 2022-10-10
4+
weight = 0
5+
[extra]
6+
lesson_date = 2022-10-10
7+
+++
8+
9+
# JNP 3: Rust
10+
11+
We will be using [Github Classroom](https://classroom.github.com) for task submission and [Discord](https://discord.gg/j2JFAsj7) for discussions.
12+
13+
Our main learning/teaching resource will be ["The Book"](https://doc.rust-lang.org/stable/book/).
14+
15+
## Grading
16+
17+
- 1/3 of the grade is based on small tasks. There will be approximately 1 task every two weeks and each task will be graded on a scale of 0 to 3.
18+
- 2/3 of the grade is based on a big project. You can choose a topic yourself, but it must be accepted by me. The project has to be split into two parts. It can be done in groups of two (or bigger).
19+
- The grade may be increased by a bonus. You can get a bonus for:
20+
- Making a presentation about some advanced topic (const generics, futures, macros, etc.) or about architecture of a selected Rust open-source library
21+
- Contributing to a selected Rust open-source library
22+
- Contributing to this course's materials
23+
- Quizzes, homeworks, etc.
24+
25+
## Project Deadlines
26+
27+
1. 2022-11-11: Project ideas should be presented to me for further refining. If you wish to pair up with someone, now is the time to tell me.
28+
2. 2022-11-18: Final project ideas should be accepted by now.
29+
3. 2022-12-16: Deadline for submitting the first part of the project.
30+
4. 2023-01-13: Deadline for submitting the second and final part of the project.
Loading
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![allow(unused_variables)]
2+
3+
fn main() {
4+
let x = 42;
5+
6+
if x == 42 {
7+
println!("x is 42");
8+
} else if x == 43 {
9+
println!("x is 43");
10+
} else {
11+
println!("x is not 42 or 43");
12+
}
13+
14+
// we can also use ifs as expressions
15+
let a_or_b = if x == 0 {
16+
"a" // notice no semicolon at the end
17+
} else {
18+
"b"
19+
};
20+
}
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <stdio.h>
2+
3+
#define LENGTH 3
4+
5+
int main() {
6+
//# Syntax error
7+
// printf("hello world";
8+
9+
//char* array[LENGTH] = {"hello", "ancient", "world!"};
10+
11+
//# Array out-of-bounds with a statically known index
12+
// printf("%s\n", array[LENGTH]);
13+
14+
//# Array out-of-bounds with a dynamically computed index
15+
// for (int i = 0; i <= LENGTH; i++) {
16+
// printf("%s\n", array[i]);
17+
// }
18+
19+
//# Doing God knows what
20+
// char* format = "a very innocent hello %s";
21+
// printf(format);
22+
23+
//# Division by zero
24+
// int joy_division = 1/0;
25+
26+
// int joy = 0;
27+
// int joy_division = 1/joy;
28+
29+
// int joy = 0 ? 1 : 0;
30+
// int joy_division = 1/joy;
31+
32+
// printf("joy division equals %d", joy_division);
33+
34+
return 0;
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <cstdio>
3+
#include <array>
4+
5+
using std::cout;
6+
using std::endl;
7+
8+
int main() {
9+
//# Syntax error
10+
// cout < "hello world";
11+
12+
// constexpr int length = 3;
13+
// std::array<std::string, length> array = {"hello", "old", "world!"};
14+
15+
//# Array access with an out of bounds index and bounds checking during compile time
16+
// cout << std::get<length>(array) << endl;
17+
18+
//# Array access with an out of bounds index and bounds checking during runtime
19+
// cout << array.at(length) << endl;
20+
21+
//# Most common access without any checks
22+
// cout << array[length] << endl;
23+
24+
//# Array out-of-bounds with a dynamically computed index
25+
// for (int i = 0; i <= length; i++) {
26+
// cout << array.at(i) << endl;
27+
// }
28+
29+
//# This will be there in Clang 14 ...
30+
// std::string format = std::format("a very innocent hello {}");
31+
// cout << format << endl;
32+
33+
//# ... but for now, this is doing God knows what
34+
// const char* format = "a very innocent hello %s";
35+
// printf(format);
36+
37+
//# Division by zero
38+
// int joy_division = 1/0;
39+
40+
// int joy = 0;
41+
// int joy_division = 1/joy;
42+
43+
// int joy = false ? 1 : 0;
44+
// int joy_division = 1/joy;
45+
46+
// cout << "joy division equals " << joy_division << endl;
47+
48+
return 0;
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fn main() {
2+
//# Syntax error
3+
// println("hello world");
4+
5+
// let array = ["hello", "new", "world!"];
6+
7+
//# Array out-of-bounds with a statically known index
8+
// println!("{}", array[3]);
9+
10+
//# Array out-of-bounds with a dynamically computed index
11+
// for i in 0..=array.len() {
12+
// println!("{}", array[i]);
13+
// }
14+
15+
//# An unsuccessful attempt at emulating C++'s ability to read the memory we're not supposed to access
16+
// let format = "a very innocent hello {}";
17+
// println!(format);
18+
19+
//# Division by zero
20+
// let joy_division = 0/0;
21+
22+
// let joy = 0;
23+
// let joy_division = 0/joy;
24+
25+
// let joy = if false {1} else {0};
26+
// let joy_division = 0/joy;
27+
28+
// println!("{}", joy_division);
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn get_5() -> u32 {
2+
5 // we could also write "return 5;"
3+
}
4+
5+
fn print_sum(a: u32, b: u32) {
6+
println!("a + b = {}", a + b);
7+
}
8+
9+
fn main() {
10+
let a = 100;
11+
print_sum(a, get_5());
12+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let name = "World";
3+
println!("Hello, {}!", name); // using the println! macro
4+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
+++
2+
title = "Introduction to Rust"
3+
date = 2022-10-10
4+
weight = 1
5+
[extra]
6+
lesson_date = 2022-10-10
7+
+++
8+
9+
![Logo](https://www.rust-lang.org/logos/rust-logo-blk.svg)
10+
11+
# A language empowering everyone to build reliable and efficient software.
12+
13+
([unofficial logo](https://rustacean.net/))
14+
15+
## Why use Rust?
16+
17+
- It is **safe** (compared to C++ for example, as we will see in a minute)
18+
- It is **fast** (because it is compiled to machine code)
19+
- It is ergonomic and pleasant to use (static typing, expressive type system, helpful compiler
20+
warnings)
21+
- It
22+
is [loved by programmers](https://insights.stackoverflow.com/survey/2021#section-most-loved-dreaded-and-wanted-programming-scripting-and-markup-languages)
23+
- It provides excellent tooling
24+
25+
## Why learn Rust?
26+
27+
Even if you don't end up using Rust, learning it expands your horizons
28+
29+
- it helps especially with the awareness of what you can and can't do in concurrent applications
30+
- it helps you understand memory management and learn its good practices
31+
32+
## Why not to learn Rust?
33+
34+
- Some people say Rust is too hard to learn because of the borrow checker
35+
- Once you get to know Cargo you won't ever want to use a language without a built-in package
36+
manager ;)
37+
- You will start hating C++
38+
39+
## Demos
40+
41+
![Meme](cpp_meme.jpg)
42+
43+
Let's compare the same code written in [C](errors_demo.c), [C++](errors_demo.cpp)
44+
and [Rust](errors_demo.rs).
45+
46+
## Code you sent me!
47+
48+
### Aleksander Tudruj
49+
50+
{{ include_code_sample(path="lessons/01_introduction/students/tudruj.cpp", language="cpp") }}
51+
52+
### Krystyna Gasińska
53+
54+
{{ include_code_sample(path="lessons/01_introduction/students/gasinska.py", language="python") }}
55+
56+
### Antoni Koszowski
57+
58+
{{ include_code_sample(path="lessons/01_introduction/students/koszowski.go", language="go") }}
59+
60+
### Mieszko Grodzicki
61+
62+
{{ include_code_sample(path="lessons/01_introduction/students/grodzicki.py", language="python") }}
63+
64+
## Installing Rust
65+
66+
- [Rustup](https://rustup.rs/)
67+
- Setup an IDE
68+
- [CLion](https://www.jetbrains.com/clion/) (you can get
69+
it [for free](https://www.jetbrains.com/community/education/))
70+
and [Intellij-Rust](https://intellij-rust.github.io/)
71+
- [VSCode](https://code.visualstudio.com/)
72+
and [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer)
73+
- rust-analyzer also works
74+
with [other IDEs](https://rust-analyzer.github.io/manual.html#installation)
75+
76+
## Useful tools
77+
78+
![Clippy](clippy.jpg)
79+
80+
- `cargo clippy` (for static analysis)
81+
- there's also `cargo check`, but it's less powerful than clippy
82+
- `cargo fmt` (for code formatting)
83+
84+
### Rust Playground
85+
86+
- [online Rust compiler](https://play.rust-lang.org/)
87+
88+
## Hello world
89+
90+
{{ include_code_sample(path="lessons/old/2021L/01_introduction/hello_world.rs", language="rust") }}
91+
92+
### Variables
93+
94+
{{ include_code_sample(path="lessons/old/2021L/01_introduction/variables.rs", language="rust") }}
95+
96+
### Conditionals
97+
98+
{{ include_code_sample(path="lessons/old/2021L/01_introduction/conditionals.rs", language="rust") }}
99+
100+
### Loops
101+
102+
{{ include_code_sample(path="lessons/old/2021L/01_introduction/loops.rs", language="rust") }}
103+
104+
### Functions
105+
106+
{{ include_code_sample(path="lessons/old/2021L/01_introduction/functions.rs", language="rust") }}
107+
108+
## Test assignment (not graded)
109+
110+
Click [here](https://classroom.github.com/a/P_z-gHH-)
111+
112+
## Obligatory reading
113+
114+
- [The Book, chapters 1-3](https://doc.rust-lang.org/stable/book/)
115+
116+
## Additional reading
117+
118+
- [Rust By Example](https://doc.rust-lang.org/stable/rust-by-example/)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#![allow(unused_variables)]
2+
3+
fn main() {
4+
for i in 0..10 {
5+
println!("i is {}", i); // i in [0, 10)
6+
}
7+
8+
let mut x = 0;
9+
10+
while x < 50 {
11+
x += 1;
12+
}
13+
14+
let mut y = 0;
15+
let mut iterations = 0;
16+
loop {
17+
iterations += 1;
18+
if iterations % 2 == 0 {
19+
continue;
20+
}
21+
y += 1;
22+
if y == 10 {
23+
break;
24+
}
25+
}
26+
27+
// we can use labels to refer to a specific loop
28+
let mut count = 0;
29+
'counting_up: loop {
30+
let mut remaining = 10;
31+
32+
loop {
33+
if remaining == 9 {
34+
break;
35+
}
36+
if count == 2 {
37+
break 'counting_up; // ends the outer loop
38+
}
39+
remaining -= 1;
40+
}
41+
42+
count += 1;
43+
}
44+
45+
// We can use break with a value.
46+
// Because loops are expressions too,
47+
// the value we break with will be returned from the functions
48+
let mut counter = 0;
49+
let value = loop {
50+
counter += 1;
51+
if counter == 10 {
52+
break 32;
53+
}
54+
};
55+
}

0 commit comments

Comments
 (0)