我遵循Rust by Example教程,並在Tuples activity的第二部分中添加使用reverse
函數作爲模板的transpose
函數。這將接受一個矩陣作爲參數,並返回一個矩陣,其中兩個元素已被交換。例如:如何通過示例從Rust生成轉置函數示例?
println!("Matrix:\n{}", matrix);
println!("Transpose:\n{}", transpose(matrix));
預期結果:
Input Matrix:
(1.1 1.2 2.1 2.2)
Transposed output:
(1.1 2.1 1.2 2.2)
我無法找到正確的代碼,這裏就是我想:
// this is defined in the tutorial
#[derive(Debug)]
struct Matrix(f32, f32, f32, f32);
// this is my attempt that does not compile
fn transpose(maat: Matrix) -> (Matrix) {
let matrix = maat;
(matrix.0, matrix.2, matrix.1, matrix.3)
}
爲了加快我們的幫助:你的代碼是什麼輸出/出了什麼問題? – birryree
請查看如何創建[MCVE]。例如,絕大多數'main'函數對你的問題沒有影響。 – Shepmaster