Solution
// Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
/// Calculate the magnitude of the given vector.
fn magnitude(vector: &[f64; 3]) -> f64 {
let mut mag_squared = 0.0;
for coord in vector {
mag_squared += coord * coord;
}
mag_squared.sqrt()
}
/// Change the magnitude of the vector to 1.0 without changing its direction.
fn normalize(vector: &mut [f64; 3]) {
let mag = magnitude(vector);
for item in vector {
*item /= mag;
}
}
fn main() {
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
let mut v = [1.0, 2.0, 9.0];
println!("Magnitude of {v:?}: {}", magnitude(&v));
normalize(&mut v);
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
}
-
Note that in
normalizewe were able to do*item /= magto modify each element. This is because we’re iterating using a mutable reference to an array, which causes theforloop to give mutable references to each element. -
It is also possible to take slice references here, e.g.,
fn magnitude(vector: &[f64]) -> f64. This makes the function more general, at the cost of a runtime length check.