Skip to content

Commit

Permalink
Merge branch 'slides/209-convert_ascii_art_to_svg' into 'master'
Browse files Browse the repository at this point in the history
Convert ASCII art images to SVG files

See merge request feng/training/material!292
  • Loading branch information
frank-at-adacore committed Feb 21, 2025
2 parents ed8daaa + 8c31784 commit aece7ca
Show file tree
Hide file tree
Showing 13 changed files with 3,576 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,14 @@ Let's write a simple recursive evaluator for arithmetic expressions.
An example of a small arithmetic expression could be :rust:`10 + 20`, which
evaluates to :rust:`30`. We can represent the expression as a tree:

.. code:: bob
.-------.
.------ | + | ------.
| '-------' |
v v
.--------. .--------.
| 10 | | 20 |
'--------' '--------'
.. image:: comprehensive_rust_training/pattern_matching_exercise_1.svg
:width: 40%

A bigger and more complex expression would be
:rust:`(10 * 9) + ((3 - 4) * 5)`, which evaluate to :rust:`85`. We represent
this as a much bigger tree:

.. code:: bob
.-----.
.---------------- | + | ----------------.
| '-----' |
v v
.-----. .-----.
.---- | * | ----. .---- | * | ----.
| '-----' | | '-----' |
v v v v
.------. .-----. .-----. .-----.
| 10 | | 9 | .---- | "-"| ----. | 5 |
'------' '-----' | '-----' | '-----'
v v
.-----. .-----.
| 3 | | 4 |
'-----' '-----'
.. image:: comprehensive_rust_training/pattern_matching_exercise_2.svg

In code, we will represent the tree with two types:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ dynamically sized data, the actual string, on the heap:
let s1 = String::from("Hello");
}
.. code:: bob
Stack
.- - - - - - - - - - - - - -. Heap
: : .- - - - - - - - - - - - - - - -.
: s1 : : :
: +-----------+-------+ : : :
: | capacity | 5 | : : +----+----+----+----+----+ :
: | ptr | o-+---+-----+-->| H | e | l | l | o | :
: | len | 5 | : : +----+----+----+----+----+ :
: +-----------+-------+ : : :
: : : :
`- - - - - - - - - - - - - -' `- - - - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/review_of_program_memory.svg

---------
Details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ An assignment will transfer *ownership* between variables:
.. code:: rust
fn main() {
let s1: String = String::from("Hello!");
let s1: String = String::from("Hello");
let s2: String = s1;
println!("s2: {s2}");
// println!("s1: {s1}");
Expand All @@ -24,43 +24,11 @@ An assignment will transfer *ownership* between variables:

Before move to :rust:`s2`:

.. code:: bob
Stack Heap
.- - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - -.
: : : :
: s1 : : :
: +-----------+-------+ : : +----+----+----+----+----+----+ :
: | ptr | o---+---+-----+-->| H | e | l | l | o | ! | :
: | len | 6 | : : +----+----+----+----+----+----+ :
: | capacity | 6 | : : :
: +-----------+-------+ : : :
: : `- - - - - - - - - - - - - - - - - - -'
: :
`- - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/review_of_program_memory.svg

After move to :rust:`s2`:

.. code:: bob
Stack Heap
.- - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - -.
: : : :
: s1 "(inaccessible)" : : :
: +-----------+-------+ : : +----+----+----+----+----+----+ :
: | ptr | o---+---+--+--+-->| H | e | l | l | o | ! | :
: | len | 6 | : | : +----+----+----+----+----+----+ :
: | capacity | 6 | : | : :
: +-----------+-------+ : | : :
: : | `- - - - - - - - - - - - - - - - - - -'
: s2 : |
: +-----------+-------+ : |
: | ptr | o---+---+--'
: | len | 6 | :
: | capacity | 6 | :
: +-----------+-------+ :
: :
`- - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/move_semantics_2.svg

When you pass a value to a function, the value is assigned to the
function parameter. This transfers ownership:
Expand Down Expand Up @@ -109,6 +77,10 @@ In the :rust:`say_hello` example:
making move semantics the default, and by forcing programmers to make
clones explicit.

=================
More to Explore
=================

--------------------------------
Defensive Copies in Modern C++
--------------------------------
Expand All @@ -117,7 +89,7 @@ Modern C++ solves this differently:

.. code:: cpp
std::string s1 = "Cpp";
std::string s1 = "Hello";
std::string s2 = s1; // Duplicate the data in s1.
- The heap data from :rust:`s1` is duplicated and :rust:`s2` gets its own
Expand All @@ -127,42 +99,11 @@ Modern C++ solves this differently:

Before copy-assignment:

.. code:: bob
Stack Heap
.- - - - - - - - - - - - - -. .- - - - - - - - - - - -.
: : : :
: s1 : : :
: +-----------+-------+ : : +----+----+----+ :
: | ptr | o---+---+--+--+-->| C | p | p | :
: | len | 3 | : : +----+----+----+ :
: | capacity | 3 | : : :
: +-----------+-------+ : : :
: : `- - - - - - - - - - - -'
`- - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/review_of_program_memory.svg

After copy-assignment:

.. code:: bob
Stack Heap
.- - - - - - - - - - - - - -. .- - - - - - - - - - - -.
: : : :
: s1 : : :
: +-----------+-------+ : : +----+----+----+ :
: | ptr | o---+---+--+--+-->| C | p | p | :
: | len | 3 | : : +----+----+----+ :
: | capacity | 3 | : : :
: +-----------+-------+ : : :
: : : :
: s2 : : :
: +-----------+-------+ : : +----+----+----+ :
: | ptr | o---+---+-----+-->| C | p | p | :
: | len | 3 | : : +----+----+----+ :
: | capacity | 3 | : : :
: +-----------+-------+ : : :
: : `- - - - - - - - - - - -'
`- - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/copy_assignment_2.svg

Key points:

Expand Down
27 changes: 3 additions & 24 deletions courses/comprehensive_rust_training/140_smart_pointers/01_box.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@ owned pointer to data on the heap:
println!("five: {}", *five);
}
.. code:: bob
Stack Heap
.- - - - - - -. .- - - - - - -.
: : : :
: five : : :
: +-----+ : : +-----+ :
: | o---|---+-----+-->| 5 | :
: +-----+ : : +-----+ :
: : : :
: : : :
`- - - - - - -' `- - - - - - -'
.. image:: comprehensive_rust_training/smart_pointers_box_1.svg
:width: 50%

:rust:`Box<T>` implements :rust:`Deref<Target = T>`, which means that you can
:url:`call methods from T directly on a Box<T> <https://doc.rust-lang.org/std/ops/trait.Deref.html#more-on-deref-coercion>`.
Expand All @@ -52,18 +42,7 @@ indirection:
println!("{list:?}");
}
.. code:: bob
Stack Heap
.- - - - - - - - - - - - - - . .- - - - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: list : : :
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
: | Element | 1 | o--+----+-----+--->| Element | 2 | o--+--->| Nil | // | // | :
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
: : : :
: : : :
'- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/smart_pointers_box_2.svg

---------
Details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,7 @@ like :rust:`Box` to create an owned trait object: :rust:`Box<dyn Pet>`.
Memory layout after allocating :rust:`pets`:

.. code:: bob
Stack Heap
.- - - - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - -.
: : : :
: "pets: Vec<Box<dyn Pet>>" : : "data: Cat" +----+----+----+----+ :
: +-----------+-------+ : : +-------+-------+ | F | i | d | o | :
: | ptr | o---+-------+--. : | lives | 9 | +----+----+----+----+ :
: | len | 2 | : | : +-------+-------+ ^ :
: | capacity | 2 | : | : ^ | :
: +-----------+-------+ : | : | '-------. :
: : | : | data:"Dog"| :
: : | : | +-------+--|-------+ :
`- - - - - - - - - - - - - - - -' | : +---|-+-----+ | name | o, 4, 4 | :
`--+-->| o o | o o-|----->| age | 5 | :
: +-|---+-|---+ +-------+----------+ :
: | | :
`- - -| - - |- - - - - - - - - - - - - - - - -'
| |
| | "Program text"
.- - -| - - |- - - - - - - - - - - - - - - - -.
: | | vtable :
: | | +----------------------+ :
: | `----->| "<Dog as Pet>::talk" | :
: | +----------------------+ :
: | vtable :
: | +----------------------+ :
: '----------->| "<Cat as Pet>::talk" | :
: +----------------------+ :
: :
'- - - - - - - - - - - - - - - - - - - - - - -'
.. image:: comprehensive_rust_training/smart_pointers_owned_objects.svg

---------
Details
Expand Down
Loading

0 comments on commit aece7ca

Please sign in to comment.