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

Slides/199 convert rust slides to feng format #483

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions contrib/rst_files_with_prelude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ courses/static_analysis_via_compiler/*.rst
courses/gnattest/*.rst
courses/gnat_project_facility/*.rst
courses/gnatcoverage/*.rst
courses/rust_essentials/*.rst
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;
}
}

Loading