Skip to content

Commit 85ad7e3

Browse files
Merge remote-tracking branch 'origin/slides/217-create_gnatstub_training_module' into slides/209-process_include_statements
2 parents 745c53a + d346c92 commit 85ad7e3

33 files changed

+1101
-269
lines changed

contrib/rst_files_with_prelude.txt

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ courses/gnat_project_facility/*.rst
1111
courses/gnatcoverage/*.rst
1212
courses/rust_essentials/*.rst
1313
courses/comprehensive_rust_training/*.rst
14+
courses/misc_tools/*.rst

courses/comprehensive_rust_training/030_types_and_values.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ Types And Values
4141
.. include:: 030_types_and_values/03_values.rst
4242
.. include:: 030_types_and_values/04_arithmetic.rst
4343
.. include:: 030_types_and_values/05_inference.rst
44-
.. include:: 030_types_and_values/06_exercise.rst
44+
.. include:: 030_types_and_values/99_lab.rst

courses/comprehensive_rust_training/030_types_and_values/06_exercise.rst courses/comprehensive_rust_training/030_types_and_values/99_lab.rst

+26-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Exercise: Fibonacci
33
=====================
44

55
---------------------
6-
Exercise: Fibonacci
6+
Fibonacci Problem
77
---------------------
88

99
The Fibonacci sequence begins with :rust:`[0,1]`. For n>1, the n'th
@@ -13,9 +13,9 @@ n-2'th Fibonacci numbers.
1313
Write a function :rust:`fib(n)` that calculates the n'th Fibonacci number.
1414
When will this function panic?
1515

16-
::
16+
.. code:: rust
1717
18-
{{#include exercise.rs:fib}}
18+
fn fib(n: u32) -> u32 {
1919
if n < 2 {
2020
// The base case.
2121
return todo!("Implement this");
@@ -25,4 +25,26 @@ When will this function panic?
2525
}
2626
}
2727
28-
{{#include exercise.rs:main}}
28+
fn main() {
29+
let n = 20;
30+
println!("fib({n}) = {}", fib(n));
31+
}
32+
33+
---------------------
34+
Fibonacci Solution
35+
---------------------
36+
37+
.. code:: rust
38+
39+
fn fib(n: u32) -> u32 {
40+
if n < 2 {
41+
return n;
42+
} else {
43+
return fib(n - 1) + fib(n - 2);
44+
}
45+
}
46+
47+
fn main() {
48+
let n = 20;
49+
println!("fib({n}) = {}", fib(n));
50+
}

courses/comprehensive_rust_training/040_control_flow_basics.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ Control Flow Basics
4343
.. include:: 040_control_flow_basics/05_blocks_and_scopes.rst
4444
.. include:: 040_control_flow_basics/06_functions.rst
4545
.. include:: 040_control_flow_basics/07_macros.rst
46-
.. include:: 040_control_flow_basics/08_exercise.rst
46+
.. include:: 040_control_flow_basics/99_lab.rst

courses/comprehensive_rust_training/040_control_flow_basics/08_exercise.rst courses/comprehensive_rust_training/040_control_flow_basics/99_lab.rst

+30-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Exercise: Collatz Sequence
33
============================
44

55
----------------------------
6-
Exercise: Collatz Sequence
6+
Collatz Sequence Problem
77
----------------------------
88

99
The
@@ -28,12 +28,37 @@ For example, beginning with ``n1`` = 3:
2828
Write a function to calculate the length of the collatz sequence for a
2929
given initial :rust:`n`.
3030

31-
::
31+
.. code:: rust
3232
33-
{{#include exercise.rs:collatz_length}}
33+
/// Determine the length of the
34+
/// collatz sequence beginning at `n`.
35+
fn collatz_length(mut n: i32) -> u32 {
3436
todo!("Implement this")
3537
}
3638
37-
{{#include exercise.rs:tests}}
39+
fn main() {
40+
// should be 15
41+
println!("Length: {}", collatz_length(11));
42+
}
43+
44+
----------------------------
45+
Collatz Sequence Solution
46+
----------------------------
3847

39-
{{#include exercise.rs:main}}
48+
.. code:: rust
49+
50+
/// Determine the length of the
51+
/// collatz sequence beginning at `n`.
52+
fn collatz_length(mut n: i32) -> u32 {
53+
let mut len = 1;
54+
while n > 1 {
55+
n = if n % 2 == 0 { n / 2 } else { 3 * n + 1 };
56+
len += 1;
57+
}
58+
len
59+
}
60+
61+
fn main() {
62+
// should be 15
63+
println!("Length: {}", collatz_length(11));
64+
}

courses/comprehensive_rust_training/050_tuples_and_arrays.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ Tuples And Arrays
4040
.. include:: 050_tuples_and_arrays/02_tuples.rst
4141
.. include:: 050_tuples_and_arrays/03_iteration.rst
4242
.. include:: 050_tuples_and_arrays/04_destructuring.rst
43-
.. include:: 050_tuples_and_arrays/05_exercise.rst
43+
.. include:: 050_tuples_and_arrays/99_lab.rst

courses/comprehensive_rust_training/050_tuples_and_arrays/05_exercise.rst

-54
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
=========================
2+
Exercise: Nested Arrays
3+
=========================
4+
5+
-------------------------
6+
Nested Arrays Problem
7+
-------------------------
8+
9+
Arrays can contain other arrays:
10+
11+
.. code:: rust
12+
13+
let array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
14+
15+
What is the type of this variable?
16+
17+
Use an array such as the above to write a function :rust:`transpose` which
18+
will transpose a matrix (turn rows into columns):
19+
20+
Transpose
21+
22+
.. math::
23+
24+
\begin{bmatrix}
25+
1 & 2 & 3 \\
26+
4 & 5 & 6 \\
27+
7 & 8 & 9
28+
\end{bmatrix}
29+
30+
into
31+
32+
.. math::
33+
34+
\begin{bmatrix}
35+
1 & 4 & 7 \\
36+
2 & 5 & 8 \\
37+
3 & 6 & 9
38+
\end{bmatrix}
39+
40+
Copy the code below to https://play.rust-lang.org/ and implement the
41+
function. This function only operates on 3x3 matrices.
42+
43+
.. code:: rust
44+
45+
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
46+
todo!()
47+
}
48+
49+
fn main() {
50+
let matrix = [
51+
[101, 102, 103], // comment makes rustfmt add newline
52+
[201, 202, 203],
53+
[301, 302, 303],
54+
];
55+
56+
dbg!(matrix);
57+
let transposed = transpose(matrix);
58+
dbg!(transposed);
59+
}
60+
61+
-------------------------
62+
Nested Arrays Solution
63+
-------------------------
64+
65+
.. code:: rust
66+
67+
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
68+
let mut result = [[0; 3]; 3];
69+
for i in 0..3 {
70+
for j in 0..3 {
71+
result[j][i] = matrix[i][j];
72+
}
73+
}
74+
result
75+
}
76+
77+
fn main() {
78+
let matrix = [
79+
[101, 102, 103], // comment makes rustfmt add newline
80+
[201, 202, 203],
81+
[301, 302, 303],
82+
];
83+
84+
dbg!(matrix);
85+
let transposed = transpose(matrix);
86+
dbg!(transposed);
87+
}

courses/comprehensive_rust_training/060_references.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ References
4141
.. include:: 060_references/03_slices.rst
4242
.. include:: 060_references/04_strings.rst
4343
.. include:: 060_references/05_dangling.rst
44-
.. include:: 060_references/06_exercise.rst
44+
.. include:: 060_references/99_lab.rst

courses/comprehensive_rust_training/060_references/06_exercise.rst

-34
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
====================
2+
Exercise: Geometry
3+
====================
4+
5+
--------------------
6+
Geometry Problem
7+
--------------------
8+
9+
We will create a few utility functions for 3-dimensional geometry,
10+
representing a point as :rust:`[f64;3]`. It is up to you to determine the
11+
function signatures.
12+
13+
::
14+
15+
// Calculate the magnitude of a vector by summing the squares of its coordinates
16+
// and taking the square root. Use the `sqrt()` method to calculate the square
17+
// root, like `v.sqrt()`.
18+
19+
fn magnitude(...) -> f64 {
20+
todo!()
21+
}
22+
23+
// Normalize a vector by calculating its magnitude and dividing all of its
24+
// coordinates by that magnitude.
25+
26+
fn normalize(...) {
27+
todo!()
28+
}
29+
30+
// Use the following `main` to test your work.
31+
32+
fn main() {
33+
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
34+
35+
let mut v = [1.0, 2.0, 9.0];
36+
println!("Magnitude of {v:?}: {}", magnitude(&v));
37+
normalize(&mut v);
38+
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
39+
}
40+
41+
--------------------
42+
Geometry Solution
43+
--------------------
44+
45+
.. code:: rust
46+
47+
/// Calculate the magnitude of the given vector.
48+
fn magnitude(vector: &[f64; 3]) -> f64 {
49+
let mut mag_squared = 0.0;
50+
for coord in vector {
51+
mag_squared += coord * coord;
52+
}
53+
mag_squared.sqrt()
54+
}
55+
56+
/// Change the magnitude of the vector to 1.0 without changing its direction.
57+
fn normalize(vector: &mut [f64; 3]) {
58+
let mag = magnitude(vector);
59+
for item in vector {
60+
*item /= mag;
61+
}
62+
}
63+
64+
fn main() {
65+
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
66+
67+
let mut v = [1.0, 2.0, 9.0];
68+
println!("Magnitude of {v:?}: {}", magnitude(&v));
69+
normalize(&mut v);
70+
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
71+
}
72+
73+
------------------------
74+
Additional Information
75+
------------------------
76+
77+
Note that in :rust:`normalize` we wrote :rust:`*item /= mag` to modify each element.
78+
79+
This is because we’re iterating using a mutable reference to an array, which causes the :rust:`for` loop to give mutable references to each element.

courses/comprehensive_rust_training/070_user_defined_types.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,4 @@ User Defined Types
4242
.. include:: 070_user_defined_types/04_aliases.rst
4343
.. include:: 070_user_defined_types/05_const.rst
4444
.. include:: 070_user_defined_types/06_static.rst
45-
.. include:: 070_user_defined_types/07_exercise.rst
45+
.. include:: 070_user_defined_types/99_lab.rst

0 commit comments

Comments
 (0)