解答

  1. fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
  2. let mut result = [[0; 3]; 3];
  3. for i in 0..3 {
  4. for j in 0..3 {
  5. result[j][i] = matrix[i][j];
  6. }
  7. }
  8. result
  9. }
  10. #[test]
  11. fn test_transpose() {
  12. let matrix = [
  13. [101, 102, 103], //
  14. [201, 202, 203],
  15. [301, 302, 303],
  16. ];
  17. let transposed = transpose(matrix);
  18. assert_eq!(
  19. transposed,
  20. [
  21. [101, 201, 301], //
  22. [102, 202, 302],
  23. [103, 203, 303],
  24. ]
  25. );
  26. }
  27. fn main() {
  28. let matrix = [
  29. [101, 102, 103], // <-- the comment makes rustfmt add a newline
  30. [201, 202, 203],
  31. [301, 302, 303],
  32. ];
  33. println!("matrix: {:#?}", matrix);
  34. let transposed = transpose(matrix);
  35. println!("transposed: {:#?}", transposed);
  36. }