Skip to content

Commit aece7ca

Browse files
Merge branch 'slides/209-convert_ascii_art_to_svg' into 'master'
Convert ASCII art images to SVG files See merge request feng/training/material!292
2 parents ed8daaa + 8c31784 commit aece7ca

File tree

13 files changed

+3576
-163
lines changed

13 files changed

+3576
-163
lines changed

courses/comprehensive_rust_training/080_pattern_matching/05_exercise.rst

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,14 @@ Let's write a simple recursive evaluator for arithmetic expressions.
1111
An example of a small arithmetic expression could be :rust:`10 + 20`, which
1212
evaluates to :rust:`30`. We can represent the expression as a tree:
1313

14-
.. code:: bob
15-
16-
.-------.
17-
.------ | + | ------.
18-
| '-------' |
19-
v v
20-
.--------. .--------.
21-
| 10 | | 20 |
22-
'--------' '--------'
14+
.. image:: comprehensive_rust_training/pattern_matching_exercise_1.svg
15+
:width: 40%
2316

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

28-
.. code:: bob
29-
30-
.-----.
31-
.---------------- | + | ----------------.
32-
| '-----' |
33-
v v
34-
.-----. .-----.
35-
.---- | * | ----. .---- | * | ----.
36-
| '-----' | | '-----' |
37-
v v v v
38-
.------. .-----. .-----. .-----.
39-
| 10 | | 9 | .---- | "-"| ----. | 5 |
40-
'------' '-----' | '-----' | '-----'
41-
v v
42-
.-----. .-----.
43-
| 3 | | 4 |
44-
'-----' '-----'
21+
.. image:: comprehensive_rust_training/pattern_matching_exercise_2.svg
4522

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

courses/comprehensive_rust_training/130_memory_management/01_review.rst

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,7 @@ dynamically sized data, the actual string, on the heap:
3434
let s1 = String::from("Hello");
3535
}
3636
37-
.. code:: bob
38-
39-
Stack
40-
.- - - - - - - - - - - - - -. Heap
41-
: : .- - - - - - - - - - - - - - - -.
42-
: s1 : : :
43-
: +-----------+-------+ : : :
44-
: | capacity | 5 | : : +----+----+----+----+----+ :
45-
: | ptr | o-+---+-----+-->| H | e | l | l | o | :
46-
: | len | 5 | : : +----+----+----+----+----+ :
47-
: +-----------+-------+ : : :
48-
: : : :
49-
`- - - - - - - - - - - - - -' `- - - - - - - - - - - - - - - -'
37+
.. image:: comprehensive_rust_training/review_of_program_memory.svg
5038

5139
---------
5240
Details

courses/comprehensive_rust_training/130_memory_management/04_move.rst

Lines changed: 10 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ An assignment will transfer *ownership* between variables:
1111
.. code:: rust
1212
1313
fn main() {
14-
let s1: String = String::from("Hello!");
14+
let s1: String = String::from("Hello");
1515
let s2: String = s1;
1616
println!("s2: {s2}");
1717
// println!("s1: {s1}");
@@ -24,43 +24,11 @@ An assignment will transfer *ownership* between variables:
2424

2525
Before move to :rust:`s2`:
2626

27-
.. code:: bob
28-
29-
Stack Heap
30-
.- - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - -.
31-
: : : :
32-
: s1 : : :
33-
: +-----------+-------+ : : +----+----+----+----+----+----+ :
34-
: | ptr | o---+---+-----+-->| H | e | l | l | o | ! | :
35-
: | len | 6 | : : +----+----+----+----+----+----+ :
36-
: | capacity | 6 | : : :
37-
: +-----------+-------+ : : :
38-
: : `- - - - - - - - - - - - - - - - - - -'
39-
: :
40-
`- - - - - - - - - - - - - -'
27+
.. image:: comprehensive_rust_training/review_of_program_memory.svg
4128

4229
After move to :rust:`s2`:
4330

44-
.. code:: bob
45-
46-
Stack Heap
47-
.- - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - -.
48-
: : : :
49-
: s1 "(inaccessible)" : : :
50-
: +-----------+-------+ : : +----+----+----+----+----+----+ :
51-
: | ptr | o---+---+--+--+-->| H | e | l | l | o | ! | :
52-
: | len | 6 | : | : +----+----+----+----+----+----+ :
53-
: | capacity | 6 | : | : :
54-
: +-----------+-------+ : | : :
55-
: : | `- - - - - - - - - - - - - - - - - - -'
56-
: s2 : |
57-
: +-----------+-------+ : |
58-
: | ptr | o---+---+--'
59-
: | len | 6 | :
60-
: | capacity | 6 | :
61-
: +-----------+-------+ :
62-
: :
63-
`- - - - - - - - - - - - - -'
31+
.. image:: comprehensive_rust_training/move_semantics_2.svg
6432

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

80+
=================
81+
More to Explore
82+
=================
83+
11284
--------------------------------
11385
Defensive Copies in Modern C++
11486
--------------------------------
@@ -117,7 +89,7 @@ Modern C++ solves this differently:
11789

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

128100
Before copy-assignment:
129101

130-
.. code:: bob
131-
132-
Stack Heap
133-
.- - - - - - - - - - - - - -. .- - - - - - - - - - - -.
134-
: : : :
135-
: s1 : : :
136-
: +-----------+-------+ : : +----+----+----+ :
137-
: | ptr | o---+---+--+--+-->| C | p | p | :
138-
: | len | 3 | : : +----+----+----+ :
139-
: | capacity | 3 | : : :
140-
: +-----------+-------+ : : :
141-
: : `- - - - - - - - - - - -'
142-
`- - - - - - - - - - - - - -'
102+
.. image:: comprehensive_rust_training/review_of_program_memory.svg
143103

144104
After copy-assignment:
145105

146-
.. code:: bob
147-
148-
Stack Heap
149-
.- - - - - - - - - - - - - -. .- - - - - - - - - - - -.
150-
: : : :
151-
: s1 : : :
152-
: +-----------+-------+ : : +----+----+----+ :
153-
: | ptr | o---+---+--+--+-->| C | p | p | :
154-
: | len | 3 | : : +----+----+----+ :
155-
: | capacity | 3 | : : :
156-
: +-----------+-------+ : : :
157-
: : : :
158-
: s2 : : :
159-
: +-----------+-------+ : : +----+----+----+ :
160-
: | ptr | o---+---+-----+-->| C | p | p | :
161-
: | len | 3 | : : +----+----+----+ :
162-
: | capacity | 3 | : : :
163-
: +-----------+-------+ : : :
164-
: : `- - - - - - - - - - - -'
165-
`- - - - - - - - - - - - - -'
106+
.. image:: comprehensive_rust_training/copy_assignment_2.svg
166107

167108
Key points:
168109

courses/comprehensive_rust_training/140_smart_pointers/01_box.rst

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@ owned pointer to data on the heap:
1616
println!("five: {}", *five);
1717
}
1818
19-
.. code:: bob
20-
21-
Stack Heap
22-
.- - - - - - -. .- - - - - - -.
23-
: : : :
24-
: five : : :
25-
: +-----+ : : +-----+ :
26-
: | o---|---+-----+-->| 5 | :
27-
: +-----+ : : +-----+ :
28-
: : : :
29-
: : : :
30-
`- - - - - - -' `- - - - - - -'
19+
.. image:: comprehensive_rust_training/smart_pointers_box_1.svg
20+
:width: 50%
3121

3222
:rust:`Box<T>` implements :rust:`Deref<Target = T>`, which means that you can
3323
:url:`call methods from T directly on a Box<T> <https://doc.rust-lang.org/std/ops/trait.Deref.html#more-on-deref-coercion>`.
@@ -52,18 +42,7 @@ indirection:
5242
println!("{list:?}");
5343
}
5444
55-
.. code:: bob
56-
57-
Stack Heap
58-
.- - - - - - - - - - - - - - . .- - - - - - - - - - - - - - - - - - - - - - - - -.
59-
: : : :
60-
: list : : :
61-
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
62-
: | Element | 1 | o--+----+-----+--->| Element | 2 | o--+--->| Nil | // | // | :
63-
: +---------+----+----+ : : +---------+----+----+ +------+----+----+ :
64-
: : : :
65-
: : : :
66-
'- - - - - - - - - - - - - - ' '- - - - - - - - - - - - - - - - - - - - - - - - -'
45+
.. image:: comprehensive_rust_training/smart_pointers_box_2.svg
6746

6847
---------
6948
Details

courses/comprehensive_rust_training/140_smart_pointers/03_trait_objects.rst

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,7 @@ like :rust:`Box` to create an owned trait object: :rust:`Box<dyn Pet>`.
4848
4949
Memory layout after allocating :rust:`pets`:
5050

51-
.. code:: bob
52-
53-
Stack Heap
54-
.- - - - - - - - - - - - - - - -. .- - - - - - - - - - - - - - - - - - - - - - -.
55-
: : : :
56-
: "pets: Vec<Box<dyn Pet>>" : : "data: Cat" +----+----+----+----+ :
57-
: +-----------+-------+ : : +-------+-------+ | F | i | d | o | :
58-
: | ptr | o---+-------+--. : | lives | 9 | +----+----+----+----+ :
59-
: | len | 2 | : | : +-------+-------+ ^ :
60-
: | capacity | 2 | : | : ^ | :
61-
: +-----------+-------+ : | : | '-------. :
62-
: : | : | data:"Dog"| :
63-
: : | : | +-------+--|-------+ :
64-
`- - - - - - - - - - - - - - - -' | : +---|-+-----+ | name | o, 4, 4 | :
65-
`--+-->| o o | o o-|----->| age | 5 | :
66-
: +-|---+-|---+ +-------+----------+ :
67-
: | | :
68-
`- - -| - - |- - - - - - - - - - - - - - - - -'
69-
| |
70-
| | "Program text"
71-
.- - -| - - |- - - - - - - - - - - - - - - - -.
72-
: | | vtable :
73-
: | | +----------------------+ :
74-
: | `----->| "<Dog as Pet>::talk" | :
75-
: | +----------------------+ :
76-
: | vtable :
77-
: | +----------------------+ :
78-
: '----------->| "<Cat as Pet>::talk" | :
79-
: +----------------------+ :
80-
: :
81-
'- - - - - - - - - - - - - - - - - - - - - - -'
51+
.. image:: comprehensive_rust_training/smart_pointers_owned_objects.svg
8252

8353
---------
8454
Details

0 commit comments

Comments
 (0)