updates
This commit is contained in:
parent
3111ed11eb
commit
6c6eb77480
8
class/ex1/Cargo.toml
Normal file
8
class/ex1/Cargo.toml
Normal 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
45
class/ex1/src/main.rs
Normal 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);
|
||||
}
|
@ -158,3 +158,5 @@ traits!
|
||||
type conversions in a generic way
|
||||
x.into() will do the type conversion for you, if valid and lossless
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user