This commit is contained in:
LuKe Tidd 2023-05-10 16:47:23 -04:00
parent 3111ed11eb
commit 6c6eb77480
Signed by: luke
GPG Key ID: 75D6600BEF4E8E8F
3 changed files with 55 additions and 0 deletions

8
class/ex1/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "pretty_print"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

45
class/ex1/src/main.rs Normal file
View File

@ -0,0 +1,45 @@
// TODO: remove this when you're done with your implementation.
#![allow(unused_variables, dead_code)]
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
/*
[[matrix[0][0],matrix[1][0],matrix[2][0]],
[matrix[0][1],matrix[1][1],matrix[2][1]],
[matrix[0][2],matrix[1][2],matrix[2][2]]]
*/
let mut temp: [[i32; 3]; 3] = [0; 3]; 3]
for row in 0..3 { // has additional bounds checks
// for row in matrix {
// println :
// }
for col in 0..3 {
transposed[col][row] = matrix[row][col];
}
}
transposed
}
fn pretty_print(matrix: &[[i32; 3]; 3]) {
for x in 0..3 {
print!("[");
for y in 0..3 {
print!("{} ", &matrix[x][y]);
}
print!("]\n");
}
}
fn main() {
let matrix = [
[101, 102, 103], // <-- the comment makes rustfmt add a newline
[201, 202, 203],
[301, 302, 303],
];
println!("matrix:");
pretty_print(&matrix);
let transposed = transpose(matrix);
println!("transposed:");
pretty_print(&transposed);
}

View File

@ -158,3 +158,5 @@ traits!
type conversions in a generic way
x.into() will do the type conversion for you, if valid and lossless