我正在尝试通过示例网站通过 Rust上的“元组课程” ,但我被格式化的输出实现所困。我有这段代码,它打印传递的矩阵:
#[derive(Debug)]
struct Matrix{
data: Vec<Vec<f64>> // [[...], [...],]
}
impl fmt::Display for Matrix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let output_data = self.data
// [[1, 2], [2, 3]] -> ["1, 2", "2, 3"]
.into_iter()
.map(|row| {
row.into_iter()
.map(|value| value.to_string())
.collect::<Vec<String>>()
.join(", ")
})
.collect::<Vec<String>>()
// ["1, 2", "2, 3"] -> ["(1, 2)", "(2, 3)"]
.into_iter()
.map(|string_row| { format!("({})", string_row) })
// ["(1, 2)", "(2, 3)"] -> "(1, 2),\n(2, 3)"
.collect::<Vec<String>>()
.join(",\n");
write!(f, "{}", output_data)
}
}
但是编译器会打印下一条消息:
<anon>:21:40: 21:44 error: cannot move out of borrowed content [E0507]
<anon>:21 let output_data = self.data
^~~~
<anon>:21:40: 21:44 help: see the detailed explanation for E0507
error: aborting due to previous error
playpen: application terminated with error code 101
我试图将output_data
's 结果包装到 aRefCell
中,但编译器仍然会打印此错误。如何解决此问题,以便write!
宏正常工作?