Skip to content

Convert multi-day modules into a single module #484

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

Open
wants to merge 2 commits into
base: master
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
972 changes: 0 additions & 972 deletions courses/rust_essentials/010_day_one.rst

This file was deleted.

52 changes: 52 additions & 0 deletions courses/rust_essentials/010_rust_essentials.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
*****************
Rust Essentials
*****************

.. container:: PRELUDE BEGIN

.. container:: PRELUDE ROLES

.. role:: ada(code)
:language: Ada

.. role:: C(code)
:language: C

.. role:: cpp(code)
:language: C++

.. container:: PRELUDE SYMBOLS

.. |rightarrow| replace:: :math:`\rightarrow`
.. |forall| replace:: :math:`\forall`
.. |exists| replace:: :math:`\exists`
.. |equivalent| replace:: :math:`\iff`
.. |le| replace:: :math:`\le`
.. |ge| replace:: :math:`\ge`
.. |lt| replace:: :math:`<`
.. |gt| replace:: :math:`>`
.. |checkmark| replace:: :math:`\checkmark`

.. container:: PRELUDE REQUIRES

.. container:: PRELUDE PROVIDES

.. container:: PRELUDE END

.. include:: 010_rust_essentials/01-introduction_to_rust.rst
.. include:: 010_rust_essentials/02-procedural_language.rst
.. include:: 010_rust_essentials/03-language_quizzes.rst
.. include:: 010_rust_essentials/04-types.rst
.. include:: 010_rust_essentials/05-type_quizzes.rst
.. include:: 010_rust_essentials/06-functions_and_ownership.rst
.. include:: 010_rust_essentials/07-functions_and_ownership_quizzes.rst
.. include:: 010_rust_essentials/08-more_types.rst
.. include:: 010_rust_essentials/09-pattern_matching.rst
.. include:: 010_rust_essentials/10-traits_and_generics.rst
.. include:: 010_rust_essentials/11-traits_and_generics_quizzes.rst
.. include:: 010_rust_essentials/12-packages_and_modularity.rst
.. include:: 010_rust_essentials/13-functional_programming.rst
.. include:: 010_rust_essentials/14-functional_programming_quizzes.rst
.. include:: 010_rust_essentials/15-error_handling.rst
.. include:: 010_rust_essentials/16-smart_pointer_types.rst
.. include:: 010_rust_essentials/17-macros.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.. role:: rust(code)
:language: rust

======================
Introduction to Rust
======================

---------
History
---------

* 2006
* Personal project by Graydon Hoare (working @ Mozilla at the time)
* No specification, instead semantics are based on implementation
* Language changed *a lot* between 2006 and 2015 (and is still changing a lot
by other languages' standards)
* Nowadays, maintained and evolved by the Rust foundation

-------------------
High Level Vision
-------------------

* Safer alternative to C/C++ for systems programming
* Many inspirations, including ML family languages, C++
* Focus on safety, albeit with a different perspective when compared to Ada
(memory safety being the most valued kind of safety)

------------
Rust Today
------------

* Use of Rust is spreading like wildfire
* Projects like Android, Linux
* Companies like Google, Amazon
* Well positioned to become a credible alternative to C++, and maybe even C
* Big list of industrial users here: https://www.rust-lang.org/production/users

-------------------------------
In the Safety Critical Market
-------------------------------

* Rust making forays into the critical markets. Big players are assessing the use of Rust in their codebases.
* But lacking industrial support for now
* Will probably become mainstream in the coming decade

--------------------
Rust "Hello World"
--------------------

.. code:: Rust

fn main() {
println!("Hello, world!");
}

149 changes: 149 additions & 0 deletions courses/rust_essentials/010_rust_essentials/02-procedural_language.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
=====================
Procedural language
=====================

--------------------------------
First, a Note About Philosophy
--------------------------------

* In C/C++, very weak distinction between statements and expressions

- You can use exprs as statements

* In Ada, strong distinction between statements and expressions

- Statements are statements, expressions are expressions, not interchangeable
- Procedures and functions are distinct

* In Rust, everything is an expression (and you generally cannot ignore their value)

- Simpler than Ada, (much) safer than C/C++
- But not always obvious what an expression returns
- Complex type system tricks to make it work (what's the type of a loop?)

-----------
For Loops
-----------

.. code:: Rust

fn main() {
for i in 1..10 {
// ^ Range object (of type Range)
println!("Hello, World!");
}
}

-------------
While Loops
-------------

.. code:: Rust

fn main() {
let mut i = 1;
// ^ Declare a mutable variable (immutable by default)

// No parens around condition
while i < 10 {
println!("Hello, World!");
i += 1; // increment
}
}

----------------
Infinite Loops
----------------

.. code:: Rust

fn main() {
let mut i = 1;

loop {
println!("Hello, World!");
i += 1; // increment

if i == 5 {
// ^ equality operator
break;
}
}
}

---------------------------------
Infinite Loop with Return Value
---------------------------------

.. code:: Rust

fn main() {
let mut i = 1;

let mut a = 0;
let mut b = 1;

let res = loop {
let c = a + b;
a = b;
b = c;
i += 1;
if i > 12 {
break a;
}
};
println!("{}", res);
}

---------
If/Else
---------

.. code:: Rust

fn main() {
let mut i = 1;
loop {
if i == 5 || else i == 12 {
break;
} else if i < 5 && i > 2 {
println!("I = 3 or 4");
} else {
println!("Hello, World!");
}
}
}

--------------------------
If/Else As an Expression
--------------------------

.. code:: Rust

fn main() {
let number = if true { 5 } else { 6 };

let error = if true { 5 } else { "six" };
}

------------------
Match Expression
------------------

.. code:: Rust

fn main() {
let mut i = 1;

loop {
match i {
5 | 12 => break,
1..=4 => println!("i in 1..4"),
7 | 9 => break,
_ => println!("Hello, World!")
}

i += 1;
}
}

137 changes: 137 additions & 0 deletions courses/rust_essentials/010_rust_essentials/03-language_quizzes.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
==================
Language Quizzes
==================

----------------------------------------
Quiz 1: Is There a Compilation Error?
----------------------------------------

.. code:: Rust
:number-lines: 2

fn main() {
let a = loop {
println!("Pouet");
};

let b: u32 = a;
}

.. container:: animate

:color-green:`No error`

*But you may get a warning that line 7 is unreachable*

----------------------------------------
Quiz 2: Is There a Compilation Error?
----------------------------------------

.. code:: Rust
:number-lines: 2

fn main() {
let a = for n in 1..11 {
println!("Pouet");
};
}

.. container:: animate

:color-green:`No error`

----------------------------------------
Quiz 3: Is There a Compilation Error?
----------------------------------------

.. code:: Rust
:number-lines: 2

fn main() {
let a = for n in 1..11 {
println!("Pouet");
};

let b: u32 = a;
}

.. container:: animate

:color-red:`error[E0308]: mismatched types --> src/quiz.rs:7:21`

Types of :rust:`a` and :rust:`b` are not the same

----------------------------------------
Quiz 4: Is There a Compilation Error?
----------------------------------------

.. code:: Rust
:number-lines: 2

fn main() {
let mut i = 1;
loop {
println!(
"{}",
if i == 5 || i == 12 { "5 or 12" }
else { "everything else" }
);

i += 1;
};
}

.. container:: animate

:color-green:`No error`

----------------------------------------
Quiz 5: Is There a Compilation Error?
----------------------------------------

.. code:: Rust
:number-lines: 2

fn main() {
let mut i = 1;

loop {
println!(
"{}",
if i == 5 || i == 12 { "5 or 12" }
else if i == 15 { "15" }
);

i += 1;
};
}

.. container:: animate

:color-red:`error[E0317]: 'if' may be missing an 'else' clause --> src/quiz.rs:9:21`

:rust:`if` expressions without :rust:`else` evaluate to :rust:`()` which is not a valid value for :rust:`println`

----------------------------------------
Quiz 6: Is There a Compilation Error?
----------------------------------------

.. code:: Rust
:number-lines: 2

fn main() {
let mut i = 100;

while i {
i -= 1;

println!("{}", i);
}

}

.. container:: animate

:color-red:`error[E0308]: mismatched types --> src/quiz.rs:5:14`

:rust:`while` condition expects a boolean value, but :rust:`i` is an integer
Loading
Loading