Skip to content

Slides/209 use rust role #497

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 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
f9d4b47
Auto-replace ``token`` with :rust:`token`
frank-at-adacore Feb 6, 2025
4c8c375
Remove "raw" blocks
frank-at-adacore Feb 6, 2025
5cdb559
Fix rust role in module/slide titles
frank-at-adacore Feb 6, 2025
57ec8af
Fix table
frank-at-adacore Feb 6, 2025
58ffdc0
Fixe more titles
frank-at-adacore Feb 6, 2025
85f97e6
Add missing sub-modules
frank-at-adacore Feb 7, 2025
d5c8441
Fix formatting issues
frank-at-adacore Feb 7, 2025
2283e9c
Fix build errors
frank-at-adacore Feb 7, 2025
3f08997
Trying to fix build failures
frank-at-adacore Feb 7, 2025
02c1b31
Switch to unix formatting
frank-at-adacore Feb 8, 2025
2a7e57a
Trying to find failure
frank-at-adacore Feb 9, 2025
b446b29
More testing
frank-at-adacore Feb 9, 2025
7adcc80
More testing
frank-at-adacore Feb 9, 2025
5286bac
more testing
frank-at-adacore Feb 9, 2025
bc63656
Testing
frank-at-adacore Feb 9, 2025
412e6cb
Fixes?
frank-at-adacore Feb 9, 2025
328d8db
More testing
frank-at-adacore Feb 10, 2025
437351d
Still testing
frank-at-adacore Feb 10, 2025
3b919a6
More
frank-at-adacore Feb 10, 2025
6079be6
More test
frank-at-adacore Feb 10, 2025
78054cf
Still more testing
frank-at-adacore Feb 10, 2025
9132642
More
frank-at-adacore Feb 10, 2025
5707a27
Still more
frank-at-adacore Feb 10, 2025
b5c0989
Once more
frank-at-adacore Feb 10, 2025
cbf1f15
Should be it
frank-at-adacore Feb 10, 2025
f11bd8a
WTF
frank-at-adacore Feb 10, 2025
7b84f2a
Trying again
frank-at-adacore Feb 10, 2025
8c38144
OMG
frank-at-adacore Feb 10, 2025
6401f27
More
frank-at-adacore Feb 10, 2025
c31291a
Still more
frank-at-adacore Feb 10, 2025
b238b9e
Again
frank-at-adacore Feb 10, 2025
84f2ae4
boring
frank-at-adacore Feb 10, 2025
a8e076c
Last one?
frank-at-adacore Feb 10, 2025
9f706b1
Narrowing it down
frank-at-adacore Feb 10, 2025
99620ea
More
frank-at-adacore Feb 10, 2025
6e115e0
Last one?
frank-at-adacore Feb 10, 2025
c7e3347
Closer
frank-at-adacore Feb 10, 2025
5f200e5
Closer still
frank-at-adacore Feb 10, 2025
55fbaa6
Yeesh
frank-at-adacore Feb 10, 2025
6415e01
WTF
frank-at-adacore Feb 10, 2025
eca6542
...
frank-at-adacore Feb 10, 2025
e42fc59
WTF!
frank-at-adacore Feb 10, 2025
bd6d978
ridiculous
frank-at-adacore Feb 10, 2025
0c41541
...
frank-at-adacore Feb 10, 2025
62d604c
111
frank-at-adacore Feb 10, 2025
70967c1
222
frank-at-adacore Feb 10, 2025
7476d0d
333
frank-at-adacore Feb 10, 2025
7125c06
444
frank-at-adacore Feb 10, 2025
9b2a977
555
frank-at-adacore Feb 10, 2025
bdc6f04
Maybe
frank-at-adacore Feb 10, 2025
87053ca
Fixed?
frank-at-adacore Feb 10, 2025
a617532
Fixed!
frank-at-adacore Feb 10, 2025
02b143c
Fix more instances of the problem
frank-at-adacore Feb 10, 2025
089cbc7
Merge remote-tracking branch 'origin/master' into slides/209-use_rust…
frank-at-adacore Feb 11, 2025
fdf34e0
Fix line endings
frank-at-adacore Feb 11, 2025
8cb1de8
Merge remote-tracking branch 'origin/master' into slides/209-use_rust…
frank-at-adacore Feb 19, 2025
cdd83f4
Remove last "raw html" line
frank-at-adacore Feb 19, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Rust is a new programming language which had its `1.0 release in

- Rust is a statically compiled language in a similar role as C++

- ``rustc`` uses LLVM as its backend.
- :rust:`rustc` uses LLVM as its backend.

- Rust supports many `platforms and
architectures <https://doc.rust-lang.org/nightly/rustc/platform-support.html>`__:
Expand All @@ -27,8 +27,6 @@ Rust is a new programming language which had its `1.0 release in
- desktops,
- servers.

.. raw:: html

---------
Details
---------
Expand All @@ -41,6 +39,3 @@ Rust fits in the same area as C++:
microcontrollers.
- Has no runtime or garbage collection.
- Focuses on reliability and safety without sacrificing performance.

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Some unique selling points of Rust:
- No uninitialized variables.
- No double-frees.
- No use-after-free.
- No ``NULL`` pointers.
- No :rust:`NULL` pointers.
- No forgotten locked mutexes.
- No data races between threads.
- No iterator invalidation.
Expand All @@ -37,8 +37,6 @@ Some unique selling points of Rust:
- Built-in support for testing.
- Excellent Language Server Protocol support.

.. raw:: html

---------
Details
---------
Expand All @@ -60,6 +58,3 @@ Depending on the answer you can highlight different features of Rust:
language feeling. In addition you get fast and predictable
performance like C and C++ (no garbage collector) as well as access
to low-level hardware (should you need it).

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ way to run short Rust programs, and is the basis for the examples and
exercises in this course. Try running the "hello-world" program it
starts with. It comes with a few handy features:

- Under "Tools", use the ``rustfmt`` option to format your code in the
- Under "Tools", use the :rust:`rustfmt` option to format your code in the
"standard" way.

- Rust has two main "profiles" for generating code: Debug (extra
Expand All @@ -21,8 +21,6 @@ starts with. It comes with a few handy features:
- If you're interested, use "ASM" under "..." to see the generated
assembly code.

.. raw:: html

---------
Details
---------
Expand All @@ -32,6 +30,3 @@ playground and experiment a little. Encourage them to keep the tab open
and try things out during the rest of the course. This is particularly
helpful for advanced students who want to know more about Rust's
optimizations or generated assembly.

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@ Hello, World
Let us jump into the simplest possible Rust program, a classic Hello
World program:

.. code:: rust,editable
.. code:: rust

fn main() {
println!("Hello World!");
}

What you see:

- Functions are introduced with ``fn``.
- Functions are introduced with :rust:`fn`.
- Blocks are delimited by curly braces like in C and C++.
- The ``main`` function is the entry point of the program.
- Rust has hygienic macros, ``println!`` is an example of this.
- The :rust:`main` function is the entry point of the program.
- Rust has hygienic macros, :rust:`println!` is an example of this.
- Rust strings are UTF-8 encoded and can contain any Unicode character.

.. raw:: html

---------
Details
---------
Expand Down Expand Up @@ -56,6 +54,3 @@ Key points:
while it is not a functional language, it includes a range of
`functional
concepts <https://doc.rust-lang.org/book/ch13-00-functional-features.html>`__.

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ Variables
-----------

Rust provides type safety via static typing. Variable bindings are made
with ``let``:
with :rust:`let`:

.. code:: rust,editable,warnunused
.. code:: rust

fn main() {
let x: i32 = 10;
Expand All @@ -18,23 +18,18 @@ with ``let``:
// println!("x: {x}");
}

.. raw:: html

---------
Details
---------

- Uncomment the ``x = 20`` to demonstrate that variables are immutable
by default. Add the ``mut`` keyword to allow changes.
- Uncomment the :rust:`x = 20` to demonstrate that variables are immutable
by default. Add the :rust:`mut` keyword to allow changes.

- Warnings are enabled for this slide, such as for unused variables or
unnecessary ``mut``. These are omitted in most slides to avoid
unnecessary :rust:`mut`. These are omitted in most slides to avoid
distracting warnings. Try removing the mutation but leaving the
``mut`` keyword in place.
:rust:`mut` keyword in place.

- The ``i32`` here is the type of the variable. This must be known at
- The :rust:`i32` here is the type of the variable. This must be known at
compile time, but type inference (covered later) allows the
programmer to omit it in many cases.

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ each type.

The types have widths as follows:

- ``iN``, ``uN``, and ``fN`` are *N* bits wide,
- ``isize`` and ``usize`` are the width of a pointer,
- ``char`` is 32 bits wide,
- ``bool`` is 8 bits wide.

.. raw:: html
- :rust:`iN`, :rust:`uN`, and :rust:`fN` are *N* bits wide,
- :rust:`isize` and :rust:`usize` are the width of a pointer,
- :rust:`char` is 32 bits wide,
- :rust:`bool` is 8 bits wide.

---------
Details
Expand All @@ -52,8 +50,5 @@ Details
There are a few syntaxes which are not shown above:

- All underscores in numbers can be left out, they are for legibility
only. So ``1_000`` can be written as ``1000`` (or ``10_00``), and
``123_i64`` can be written as ``123i64``.

.. raw:: html

only. So :rust:`1_000` can be written as :rust:`1000` (or :rust:`10_00`), and
:rust:`123_i64` can be written as :rust:`123i64`.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Arithmetic
Arithmetic
------------

.. code:: rust,editable
.. code:: rust

fn interproduct(a: i32, b: i32, c: i32) -> i32 {
return a * b + b * c + c * a;
Expand All @@ -16,13 +16,11 @@ Arithmetic
println!("result: {}", interproduct(120, 100, 248));
}

.. raw:: html

---------
Details
---------

This is the first time we've seen a function other than ``main``, but
This is the first time we've seen a function other than :rust:`main`, but
the meaning should be clear: it takes three integers, and returns an
integer. Functions will be covered in more detail later.

Expand All @@ -32,14 +30,11 @@ What about integer overflow? In C and C++ overflow of *signed* integers
is actually undefined, and might do unknown things at runtime. In Rust,
it's defined.

Change the ``i32``\ 's to ``i16`` to see an integer overflow, which
Change the :rust:`i32` to :rust:`i16` to see an integer overflow, which
panics (checked) in a debug build and wraps in a release build. There
are other options, such as overflowing, saturating, and carrying. These
are accessed with method syntax, e.g.,
``(a * b).saturating_add(b * c).saturating_add(c * a)``.
:rust:`(a * b).saturating_add(b * c).saturating_add(c * a)`.

In fact, the compiler will detect overflow of constant expressions,
which is why the example requires a separate function.

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ Type Inference

Rust will look at how the variable is *used* to determine the type:

.. raw:: html

<!-- mdbook-xgettext: skip -->

.. code:: rust,editable
.. code:: rust

fn takes_u32(x: u32) {
println!("u32: {x}");
Expand All @@ -31,8 +27,6 @@ Rust will look at how the variable is *used* to determine the type:
// takes_u32(y);
}

.. raw:: html

---------
Details
---------
Expand All @@ -47,17 +41,14 @@ declaration of a type. The compiler does the job for us and helps us
write more concise code.

When nothing constrains the type of an integer literal, Rust defaults to
``i32``. This sometimes appears as ``{integer}`` in error messages.
Similarly, floating-point literals default to ``f64``.
:rust:`i32`. This sometimes appears as :rust:`{integer}` in error messages.
Similarly, floating-point literals default to :rust:`f64`.

.. code:: rust,compile_fail
.. code:: rust

fn main() {
let x = 3.14;
let y = 20;
assert_eq!(x, y);
// ERROR: no implementation for `{float} == {integer}`
}

.. raw:: html

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ Exercise: Fibonacci
Exercise: Fibonacci
---------------------

The Fibonacci sequence begins with ``[0,1]``. For n>1, the n'th
The Fibonacci sequence begins with :rust:`[0,1]`. For n>1, the n'th
Fibonacci number is calculated recursively as the sum of the n-1'th and
n-2'th Fibonacci numbers.

Write a function ``fib(n)`` that calculates the n'th Fibonacci number.
Write a function :rust:`fib(n)` that calculates the n'th Fibonacci number.
When will this function panic?

.. code:: rust,editable,should_panic
::

{{#include exercise.rs:fib}}
if n < 2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
====================
``if`` expressions
"if" expressions
====================

--------------------
``if`` expressions
"if" expressions
--------------------

You use
:url:`if expressions <https://doc.rust-lang.org/reference/expressions/if-expr.html#if-expressions>`
exactly like ``if`` statements in other languages:
exactly like :rust:`if` statements in other languages:

.. code:: rust,editable
.. code:: rust

fn main() {
let x = 10;
Expand All @@ -23,31 +23,26 @@ exactly like ``if`` statements in other languages:
}
}

In addition, you can use ``if`` as an expression. The last expression of
each block becomes the value of the ``if`` expression:
In addition, you can use :rust:`if` as an expression. The last expression of
each block becomes the value of the :rust:`if` expression:

.. code:: rust,editable
.. code:: rust

fn main() {
let x = 10;
let size = if x < 20 { "small" } else { "large" };
println!("number size: {}", size);
}

.. raw:: html

---------
Details
---------

Because ``if`` is an expression and must have a particular type, both of
Because :rust:`if` is an expression and must have a particular type, both of
its branch blocks must have the same type. Show what happens if you add
``;`` after ``"small"`` in the second example.

An ``if`` expression should be used in the same way as the other
expressions. For example, when it is used in a ``let`` statement, the
statement must be terminated with a ``;`` as well. Remove the ``;``
before ``println!`` to see the compiler error.

.. raw:: html
:rust:`;` after :rust:`"small"` in the second example.

An :rust:`if` expression should be used in the same way as the other
expressions. For example, when it is used in a :rust:`let` statement, the
statement must be terminated with a :rust:`;` as well. Remove the :rust:`;`
before :rust:`println!` to see the compiler error.
Loading
Loading