Skip to content

Commit

Permalink
Merge branch 'slides/216-use-formatting-instead-of-quoted-strings' in…
Browse files Browse the repository at this point in the history
…to 'master'

Resolve "Comprehensive Rust - use formatting instead of quoted strings"

Closes #216

See merge request feng/training/material!293
  • Loading branch information
frank-at-adacore committed Feb 21, 2025
2 parents 8ea1ab0 + f2ccc82 commit ed8daaa
Show file tree
Hide file tree
Showing 49 changed files with 227 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ Playground

The :url:`Rust Playground <https://play.rust-lang.org/>` provides an easy
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
exercises in this course. Try running the :command:`hello-world` program it
starts with. It comes with a few handy features:

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

- Rust has two main "profiles" for generating code: Debug (extra
runtime checks, less optimization) and Release (fewer runtime checks,
lots of optimization). These are accessible under "Debug" at the top.
- Rust has two main :dfn:`profiles` for generating code: **Debug** (extra
runtime checks, less optimization) and **Release** (fewer runtime checks,
lots of optimization). These are accessible under :menu:`Debug` at the top.

- If you're interested, use "ASM" under "..." to see the generated
- If you're interested, use :menu:`ASM` under :menu:`...` to see the generated
assembly code.

---------
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
====================
"if" expressions
====================
========================
:rust:`if` expressions
========================

--------------------
"if" expressions
--------------------
------------------------
:rust:`if` expressions
------------------------

You use
:url:`if expressions <https://doc.rust-lang.org/reference/expressions/if-expr.html#if-expressions>`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
=======================
"match" Expressions
=======================
===========================
:rust:`match` Expressions
===========================

-----------------------
"match" Expressions
-----------------------
---------------------------
:rust:`match` Expressions
---------------------------

:rust:`match` can be used to check a value against one or more options:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Loops
There are three looping keywords in Rust: :rust:`while`, :rust:`loop`, and
:rust:`for`:

-----------
"while"
-----------
---------------
:rust:`while`
---------------

The
:url:`while keyword <https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-loops>`
Expand All @@ -28,9 +28,9 @@ the condition is true.
println!("Final x: {x}");
}
-------------
"for"
-------------
-----------------
:rust:`for`
-----------------

The :rust:`for` `loop <https://doc.rust-lang.org/std/keyword.for.html>`__
iterates over ranges of values or the items in a collection:
Expand All @@ -47,19 +47,19 @@ iterates over ranges of values or the items in a collection:
}
}
---------------
"for" Details
---------------
-------------------
:rust:`for` Details
-------------------

- Under the hood :rust:`for` loops use a concept called :dfn:`iterators` to
handle iterating over different kinds of ranges/collections.
Iterators will be discussed in more detail later.
- Note that the first :rust:`for` loop only iterates to :rust:`4`. Show the
:rust:`1..=5` syntax for an inclusive range.

--------------
"loop"
--------------
------------------
:rust:`loop`
------------------

The :rust:`loop`
`statement <https://doc.rust-lang.org/std/keyword.loop.html>`__ just
Expand All @@ -78,9 +78,9 @@ loops forever, until a :rust:`break`.
}
}
----------------
"loop" Details
----------------
--------------------
:rust:`loop` Details
--------------------

- The :rust:`loop` statement works like a :rust:`while true` loop. Use it for
things like servers which will serve connections forever.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
============================
"break" and "continue"
============================
====================================
:rust:`break` and :rust:`continue`
====================================

----------------------------
"break" and "continue"
----------------------------
------------------------------------
:rust:`break` and :rust:`continue`
------------------------------------

If you want to immediately start the next iteration use
:url:`continue <https://doc.rust-lang.org/reference/expressions/loop-expr.html#continue-expressions>`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Details
- Fields of a tuple can be accessed by the period and the index of the
value, e.g. :rust:`t.0`, :rust:`t.1`.

- The empty tuple :rust:`()` is referred to as the "unit type" and
- The empty tuple :rust:`()` is referred to as the :dfn:`unit type` and
signifies absence of a return value, akin to :rust:`void` in other
languages.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ larger value into its constituent parts:
Details
---------

- The patterns used here are "irrefutable", meaning that the compiler
- The patterns used here are :dfn:`irrefutable`, meaning that the compiler
can statically verify that the value on the right of :rust:`=` has the
same structure as the pattern.
- A variable name is an irrefutable pattern that always matches any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Shared References
-------------------

A reference provides a way to access another value without taking
ownership of the value, and is also called "borrowing". Shared
ownership of the value, and is also called :dfn:`borrowing`. Shared
references are read-only, and the referenced data cannot change.

.. code:: rust
Expand All @@ -22,7 +22,7 @@ references are read-only, and the referenced data cannot change.
}
A shared reference to a type :rust:`T` has type :rust:`&T`. A reference value is
made with the :rust:`&` operator. The :rust:`*` operator "dereferences" a
made with the :rust:`&` operator. The :rust:`*` operator :dfn:`dereferences` a
reference, yielding its value.

---------
Expand All @@ -32,9 +32,9 @@ Details
- References can never be null in Rust, so null checking is not
necessary.

- A reference is said to "borrow" the value it refers to, and this is a
- A reference is said to **borrow** the value it refers to, and this is a
good model for students not familiar with pointers: code can use the
reference to access the value, but is still "owned" by the original
reference to access the value, but is still **owned** by the original
variable. The course will get into more detail on ownership in day 3.

- References are implemented as pointers, and a key advantage is that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Details

Key points:

- "Exclusive" means that only this reference can be used to access the
- :dfn:`Exclusive` means that only this reference can be used to access the
value. No other references (shared or exclusive) can exist at the
same time, and the referenced value cannot be accessed while the
exclusive reference exists. Try making an :rust:`&point.0` or changing
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
===========
"const"
===========
===============
:rust:`const`
===============

-----------
"const"
-----------
---------------
:rust:`const`
---------------

Constants are evaluated at compile time and their values are inlined
wherever they are used:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
============
"static"
============
================
:rust:`static`
================

------------
"static"
------------
----------------
:rust:`static`
----------------

Static variables will live during the whole execution of the program,
and therefore will not move:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ languages. They are used for pattern matching:
- :rust:`let else` expressions
- :rust:`while let` expressions

------------------------
"if let" expressions
------------------------
----------------------------
:rust:`if let` expressions
----------------------------

The
:url:`if let expression <https://doc.rust-lang.org/reference/expressions/if-expr.html#if-let-expressions>`
Expand All @@ -38,14 +38,14 @@ pattern:
sleep_for(0.8);
}
--------------------------
"let else" expressions
--------------------------
------------------------------
:rust:`let else` expressions
------------------------------

For the common case of matching a pattern and returning from the
function, use
:url:`let else <https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html>`.
The "else" case must diverge (:rust:`return`, :rust:`break`, or panic - anything
The :rust:`else` case must diverge (:rust:`return`, :rust:`break`, or panic - anything
but falling off the end of the block).

.. code:: rust
Expand Down Expand Up @@ -91,9 +91,9 @@ returns :rust:`Some(c)` until the string is empty, after which it will
return :rust:`None`. The :rust:`while let` lets us keep iterating through all
items.

--------
if-let
--------
----------------
:rust:`if-let`
----------------

- Unlike :rust:`match`, :rust:`if let` does not have to cover all branches.
This can make it more concise than :rust:`match`.
Expand All @@ -102,9 +102,9 @@ if-let
- Unlike :rust:`match`, :rust:`if let` does not support guard clauses for
pattern matching.

----------
let-else
----------
------------------
:rust:`let-else`
------------------

:rust:`if-let` can pile up, as shown. The :rust:`let-else` construct supports
flattening this nested code. Rewrite the awkward version for students,
Expand All @@ -130,9 +130,9 @@ The rewritten version is:
return Ok(digit);
}
-----------
while-let
-----------
-------------------
:rust:`while-let`
-------------------

- Point out that the :rust:`while let` loop will keep going as long as the
value matches the pattern.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ In code, we will represent the tree with two types:
{{#include exercise.rs:Expression}}

The :rust:`Box` type here is a smart pointer, and will be covered in detail
later in the course. An expression can be "boxed" with :rust:`Box::new` as
later in the course. An expression can be :dfn:`boxed` with :rust:`Box::new` as
seen in the tests. To evaluate a boxed expression, use the deref
operator (:rust:`*`) to "unbox" it: :rust:`eval(*boxed_expr)`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ with an :rust:`impl` block:
// race.add_lap(42);
}
The :rust:`self` arguments specify the "receiver" - the object the method
The :rust:`self` arguments specify the :dfn:`receiver` - the object the method
acts on. There are several common receivers for a method:

- :rust:`&self`: borrows the object from the caller using a shared and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Traits Details
- A trait defines a number of methods that types must have in order to
implement the trait.

- In the "Generics" segment, next, we will see how to build
- In the *Generics* segment, next, we will see how to build
functionality that is generic over all types implementing a trait.

---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Details
parameters. Here, :rust:`Foo::from("hello")` would not compile because
there is no :rust:`From<&str>` implementation for :rust:`Foo`.

- Generic traits take types as "input", while associated types are a
kind of "output" type. A trait can have multiple implementations for
- Generic traits take types as *input*, while associated types are a
kind of *output* type. A trait can have multiple implementations for
different input types.

- In fact, Rust requires that at most one implementation of a trait
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Details
- It has additional features making it more powerful.

- If someone asks, the extra feature is that the type on the left
of ":" can be arbitrary, like :rust:`Option<T>`.
of :rust:`:` can be arbitrary, like :rust:`Option<T>`.

- Note that Rust does not (yet) support specialization. For example,
given the original :rust:`duplicate`, it is invalid to add a specialized
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
================
"impl Trait"
================
====================
:rust:`impl Trait`
====================

----------------
"impl Trait"
----------------
--------------------
:rust:`impl Trait`
--------------------

Similar to trait bounds, an :rust:`impl Trait` syntax can be used in
function arguments and return values:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
===============
"dyn Trait"
===============
===================
:rust:`dyn Trait`
===================

---------------
"dyn Trait"
---------------
-------------------
:rust:`dyn Trait`
-------------------

In addition to using traits for static dispatch via generics, Rust also
supports using them for type-erased, dynamic dispatch via trait objects:
Expand Down Expand Up @@ -77,7 +77,7 @@ Details
pointer types like :rust:`Box` can also be used (this will be
demonstrated on day 3).

- At runtime, a :rust:`&dyn Pet` is represented as a "fat pointer", i.e. a
- At runtime, a :rust:`&dyn Pet` is represented as a :dfn:`fat pointer`, i.e. a
pair of two pointers: One pointer points to the concrete object that
implements :rust:`Pet`, and the other points to the vtable for the trait
implementation for that type. When calling the :rust:`talk` method on
Expand All @@ -86,5 +86,5 @@ Details
the :rust:`Dog` or :rust:`Cat` into that function. The compiler doesn't need
to know the concrete type of the :rust:`Pet` in order to do this.

- A :rust:`dyn Trait` is considered to be "type-erased", because we no
- A :rust:`dyn Trait` is considered to be :dfn:`type-erased`, because we no
longer have compile-time knowledge of what the concrete type is.
Loading

0 comments on commit ed8daaa

Please sign in to comment.