Transpose matriks, pass-by-referensi ke fungsi
// Transpose matrix, pass-by-reference to function
fn transpose_matrix(matrix: &mut Vec<Vec<i32>>) {
let last = matrix.len();
for y in 0..last {
for x in y..last {
let tmp = matrix[y][x];
matrix[y][x] = matrix[x][y];
matrix[x][y] = tmp;
}
}
}
fn print_vector(vector: &Vec<Vec<i32>>) {
println!("v {:?}", vector)
}
fn main() {
// Two dimensional vector
let mut v5 = vec![
vec![11, 12, 13, 14, 15],
vec![21, 22, 23, 24, 25],
vec![31, 32, 33, 34, 35],
vec![41, 42, 43, 44, 45],
vec![51, 52, 53, 54, 55],
];
println!("5 x 5 vector");
// &v5 is how the vector is passed by reference
print_vector(&v5);
// Passed by reference with intention to make changes
transpose_matrix(&mut v5);
println!("Transposed");
print_vector(&v5);
}
Mackerel