@@ -10,7 +10,7 @@ We will create a few utility functions for 3-dimensional geometry,
10
10
representing a point as :rust: `[f64;3] `. It is up to you to determine the
11
11
function signatures.
12
12
13
- ::
13
+ .. code :: rust
14
14
15
15
// Calculate the magnitude of a vector by summing the squares of its coordinates
16
16
// and taking the square root. Use the `sqrt()` method to calculate the square
@@ -27,48 +27,15 @@ function signatures.
27
27
todo!()
28
28
}
29
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]));
30
+ Use the following :rust: `main ` to test your work.
34
31
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
- }
32
+ .. container :: source_include 060_references/src/060_references.rs :start-after://ANCHOR-main :code:rust
40
33
41
34
--------------------
42
35
Geometry Solution
43
36
--------------------
44
37
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
- }
38
+ .. container :: source_include 060_references/src/060_references.rs :start-after://ANCHOR-solution :end-before://ANCHOR-main :code:rust
72
39
73
40
------------------------
74
41
Additional Information
0 commit comments