Skip to content

Commit 58a24da

Browse files
Merge branch 'slides/201-convert-rust-slides-to-feng-material-style' into 'master'
Resolve "Convert Rust Slides to FENG Material Style" Closes #201 See merge request feng/training/material!272
2 parents c347c03 + a6ce16d commit 58a24da

22 files changed

+2050
-1939
lines changed

courses/rust_essentials/010_day_one.rst

Lines changed: 0 additions & 972 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
*****************
2+
Rust Essentials
3+
*****************
4+
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
8+
9+
.. role:: ada(code)
10+
:language: Ada
11+
12+
.. role:: C(code)
13+
:language: C
14+
15+
.. role:: cpp(code)
16+
:language: C++
17+
18+
.. container:: PRELUDE SYMBOLS
19+
20+
.. |rightarrow| replace:: :math:`\rightarrow`
21+
.. |forall| replace:: :math:`\forall`
22+
.. |exists| replace:: :math:`\exists`
23+
.. |equivalent| replace:: :math:`\iff`
24+
.. |le| replace:: :math:`\le`
25+
.. |ge| replace:: :math:`\ge`
26+
.. |lt| replace:: :math:`<`
27+
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
29+
30+
.. container:: PRELUDE REQUIRES
31+
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
35+
36+
.. include:: 010_rust_essentials/01-introduction_to_rust.rst
37+
.. include:: 010_rust_essentials/02-procedural_language.rst
38+
.. include:: 010_rust_essentials/03-language_quizzes.rst
39+
.. include:: 010_rust_essentials/04-types.rst
40+
.. include:: 010_rust_essentials/05-type_quizzes.rst
41+
.. include:: 010_rust_essentials/06-functions_and_ownership.rst
42+
.. include:: 010_rust_essentials/07-functions_and_ownership_quizzes.rst
43+
.. include:: 010_rust_essentials/08-more_types.rst
44+
.. include:: 010_rust_essentials/09-pattern_matching.rst
45+
.. include:: 010_rust_essentials/10-traits_and_generics.rst
46+
.. include:: 010_rust_essentials/11-traits_and_generics_quizzes.rst
47+
.. include:: 010_rust_essentials/12-packages_and_modularity.rst
48+
.. include:: 010_rust_essentials/13-functional_programming.rst
49+
.. include:: 010_rust_essentials/14-functional_programming_quizzes.rst
50+
.. include:: 010_rust_essentials/15-error_handling.rst
51+
.. include:: 010_rust_essentials/16-smart_pointer_types.rst
52+
.. include:: 010_rust_essentials/17-macros.rst
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
.. role:: rust(code)
2+
:language: rust
3+
4+
======================
5+
Introduction to Rust
6+
======================
7+
8+
---------
9+
History
10+
---------
11+
12+
* 2006
13+
* Personal project by Graydon Hoare (working @ Mozilla at the time)
14+
* No specification, instead semantics are based on implementation
15+
* Language changed *a lot* between 2006 and 2015 (and is still changing a lot
16+
by other languages' standards)
17+
* Nowadays, maintained and evolved by the Rust foundation
18+
19+
-------------------
20+
High Level Vision
21+
-------------------
22+
23+
* Safer alternative to C/C++ for systems programming
24+
* Many inspirations, including ML family languages, C++
25+
* Focus on safety, albeit with a different perspective when compared to Ada
26+
(memory safety being the most valued kind of safety)
27+
28+
------------
29+
Rust Today
30+
------------
31+
32+
* Use of Rust is spreading like wildfire
33+
* Projects like Android, Linux
34+
* Companies like Google, Amazon
35+
* Well positioned to become a credible alternative to C++, and maybe even C
36+
* Big list of industrial users here: https://www.rust-lang.org/production/users
37+
38+
-------------------------------
39+
In the Safety Critical Market
40+
-------------------------------
41+
42+
* Rust making forays into the critical markets. Big players are assessing the use of Rust in their codebases.
43+
* But lacking industrial support for now
44+
* Will probably become mainstream in the coming decade
45+
46+
--------------------
47+
Rust "Hello World"
48+
--------------------
49+
50+
.. code:: Rust
51+
52+
fn main() {
53+
println!("Hello, world!");
54+
}
55+
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
=====================
2+
Procedural language
3+
=====================
4+
5+
--------------------------------
6+
First, a Note About Philosophy
7+
--------------------------------
8+
9+
* In C/C++, very weak distinction between statements and expressions
10+
11+
- You can use exprs as statements
12+
13+
* In Ada, strong distinction between statements and expressions
14+
15+
- Statements are statements, expressions are expressions, not interchangeable
16+
- Procedures and functions are distinct
17+
18+
* In Rust, everything is an expression (and you generally cannot ignore their value)
19+
20+
- Simpler than Ada, (much) safer than C/C++
21+
- But not always obvious what an expression returns
22+
- Complex type system tricks to make it work (what's the type of a loop?)
23+
24+
-----------
25+
For Loops
26+
-----------
27+
28+
.. code:: Rust
29+
30+
fn main() {
31+
for i in 1..10 {
32+
// ^ Range object (of type Range)
33+
println!("Hello, World!");
34+
}
35+
}
36+
37+
-------------
38+
While Loops
39+
-------------
40+
41+
.. code:: Rust
42+
43+
fn main() {
44+
let mut i = 1;
45+
// ^ Declare a mutable variable (immutable by default)
46+
47+
// No parens around condition
48+
while i < 10 {
49+
println!("Hello, World!");
50+
i += 1; // increment
51+
}
52+
}
53+
54+
----------------
55+
Infinite Loops
56+
----------------
57+
58+
.. code:: Rust
59+
60+
fn main() {
61+
let mut i = 1;
62+
63+
loop {
64+
println!("Hello, World!");
65+
i += 1; // increment
66+
67+
if i == 5 {
68+
// ^ equality operator
69+
break;
70+
}
71+
}
72+
}
73+
74+
---------------------------------
75+
Infinite Loop with Return Value
76+
---------------------------------
77+
78+
.. code:: Rust
79+
80+
fn main() {
81+
let mut i = 1;
82+
83+
let mut a = 0;
84+
let mut b = 1;
85+
86+
let res = loop {
87+
let c = a + b;
88+
a = b;
89+
b = c;
90+
i += 1;
91+
if i > 12 {
92+
break a;
93+
}
94+
};
95+
println!("{}", res);
96+
}
97+
98+
---------
99+
If/Else
100+
---------
101+
102+
.. code:: Rust
103+
104+
fn main() {
105+
let mut i = 1;
106+
loop {
107+
if i == 5 || else i == 12 {
108+
break;
109+
} else if i < 5 && i > 2 {
110+
println!("I = 3 or 4");
111+
} else {
112+
println!("Hello, World!");
113+
}
114+
}
115+
}
116+
117+
--------------------------
118+
If/Else As an Expression
119+
--------------------------
120+
121+
.. code:: Rust
122+
123+
fn main() {
124+
let number = if true { 5 } else { 6 };
125+
126+
let error = if true { 5 } else { "six" };
127+
}
128+
129+
------------------
130+
Match Expression
131+
------------------
132+
133+
.. code:: Rust
134+
135+
fn main() {
136+
let mut i = 1;
137+
138+
loop {
139+
match i {
140+
5 | 12 => break,
141+
1..=4 => println!("i in 1..4"),
142+
7 | 9 => break,
143+
_ => println!("Hello, World!")
144+
}
145+
146+
i += 1;
147+
}
148+
}
149+
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
==================
2+
Language Quizzes
3+
==================
4+
5+
----------------------------------------
6+
Quiz 1: Is There a Compilation Error?
7+
----------------------------------------
8+
9+
.. code:: Rust
10+
:number-lines: 2
11+
12+
fn main() {
13+
let a = loop {
14+
println!("Pouet");
15+
};
16+
17+
let b: u32 = a;
18+
}
19+
20+
.. container:: animate
21+
22+
:color-green:`No error`
23+
24+
*But you may get a warning that line 7 is unreachable*
25+
26+
----------------------------------------
27+
Quiz 2: Is There a Compilation Error?
28+
----------------------------------------
29+
30+
.. code:: Rust
31+
:number-lines: 2
32+
33+
fn main() {
34+
let a = for n in 1..11 {
35+
println!("Pouet");
36+
};
37+
}
38+
39+
.. container:: animate
40+
41+
:color-green:`No error`
42+
43+
----------------------------------------
44+
Quiz 3: Is There a Compilation Error?
45+
----------------------------------------
46+
47+
.. code:: Rust
48+
:number-lines: 2
49+
50+
fn main() {
51+
let a = for n in 1..11 {
52+
println!("Pouet");
53+
};
54+
55+
let b: u32 = a;
56+
}
57+
58+
.. container:: animate
59+
60+
:color-red:`error[E0308]: mismatched types --> src/quiz.rs:7:21`
61+
62+
Types of :rust:`a` and :rust:`b` are not the same
63+
64+
----------------------------------------
65+
Quiz 4: Is There a Compilation Error?
66+
----------------------------------------
67+
68+
.. code:: Rust
69+
:number-lines: 2
70+
71+
fn main() {
72+
let mut i = 1;
73+
loop {
74+
println!(
75+
"{}",
76+
if i == 5 || i == 12 { "5 or 12" }
77+
else { "everything else" }
78+
);
79+
80+
i += 1;
81+
};
82+
}
83+
84+
.. container:: animate
85+
86+
:color-green:`No error`
87+
88+
----------------------------------------
89+
Quiz 5: Is There a Compilation Error?
90+
----------------------------------------
91+
92+
.. code:: Rust
93+
:number-lines: 2
94+
95+
fn main() {
96+
let mut i = 1;
97+
98+
loop {
99+
println!(
100+
"{}",
101+
if i == 5 || i == 12 { "5 or 12" }
102+
else if i == 15 { "15" }
103+
);
104+
105+
i += 1;
106+
};
107+
}
108+
109+
.. container:: animate
110+
111+
:color-red:`error[E0317]: 'if' may be missing an 'else' clause --> src/quiz.rs:9:21`
112+
113+
:rust:`if` expressions without :rust:`else` evaluate to :rust:`()` which is not a valid value for :rust:`println`
114+
115+
----------------------------------------
116+
Quiz 6: Is There a Compilation Error?
117+
----------------------------------------
118+
119+
.. code:: Rust
120+
:number-lines: 2
121+
122+
fn main() {
123+
let mut i = 100;
124+
125+
while i {
126+
i -= 1;
127+
128+
println!("{}", i);
129+
}
130+
131+
}
132+
133+
.. container:: animate
134+
135+
:color-red:`error[E0308]: mismatched types --> src/quiz.rs:5:14`
136+
137+
:rust:`while` condition expects a boolean value, but :rust:`i` is an integer

0 commit comments

Comments
 (0)