fn rotate_matrix(matrix: &Matrix) -> Matrix {
    let mut matrix = matrix.clone();

    (0..matrix.len() / 2).for_each(|layer| {
        let first = layer;
        let last = matrix.len() - 1 - layer;

        (first..last).for_each(|i| {
            let offset = i - first;

            // save top
            let top = matrix[first][i];

            // left -> top
            matrix[first][i] = matrix[last - offset][first];

            // bottom -> left
            matrix[last - offset][first] = matrix[last][last - offset];

            // right -> bottom
            matrix[last][last - offset] = matrix[i][last];

            // top -> right
            matrix[i][last] = top; // right <- saved top
        });
    });

    matrix
}
// Because we're rotating teh matrix by 90x90 degrees, the easiest way to do this is to implement
// the rotation in layers. We perform a circular rotation on each layer, moving the top edge to the right edge,
// the right edge to the bottom edge, the bottom edge to the left edge, and the left edge to the top edge.
Because we're rotating teh matrix by 90x90 degrees, the easiest way to do this is to implement
the rotation in layers. We perform a circular rotation on each layer, moving the top edge to the right edge,
the right edge to the bottom edge, the bottom edge to the left edge, and the left edge to the top edge.
Because we ' re rotating teh matrix by 90x90 degrees the easiest way to do this is to implement the rotation in layers . We perform a circular rotation on each layer moving the top edge to the right edge the right edge to the bottom edge the bottom edge to the left edge and the left edge to the top edge .
fn rotate_matrix(matrix: &Matrix) -> Matrix {
    let mut matrix = matrix.clone();
    (0..matrix.len() / 2).for_each(|layer| {
        let first = layer;
        let last = matrix.len() - 1 - layer;
        (first..last).for_each(|i| {
            let offset = i - first;
            // save top
            let top = matrix[first][i];
            // left -> top
            matrix[first][i] = matrix[last - offset][first];
            // bottom -> left
            matrix[last - offset][first] = matrix[last][last - offset];
            // right -> bottom
            matrix[last][last - offset] = matrix[i][last];
            // top -> right
            matrix[i][last] = top; // right <- saved top
        });
    });
    matrix
}
fn rotate_matrix ( matrix : & Matrix ) -> Matrix { let mut matrix = matrix . clone ( ) ; ( 0 .. matrix . len ( ) / 2 ) . for_each ( | layer | { let first = layer ; let last = matrix . len ( ) - 1 - layer ; ( first .. last ) . for_each ( | i | { let offset = i - first ; // save top let top = matrix [ first ] [ i ] ; // left -> top matrix [ first ] [ i ] = matrix [ last - offset ] [ first ] ; // bottom -> left matrix [ last - offset ] [ first ] = matrix [ last ] [ last - offset ] ; // right -> bottom matrix [ last ] [ last - offset ] = matrix [ i ] [ last ] ; // top -> right matrix [ i ] [ last ] = top ; // right <- saved top } ) ; } ) ; matrix }
Because we're rotating teh matrix by 90x90 degrees, the easiest way to do this is to implement
the rotation in layers.
Because we ' re rotating teh matrix by 90x90 degrees the easiest way to do this is to implement the rotation in layers .
matrix
&Matrix
matrix
&Matrix