面试题答案
一键面试struct Matrix {
rows: usize,
cols: usize,
data: Vec<f64>,
}
impl Matrix {
fn from_vec(data: Vec<f64>, rows: usize, cols: usize) -> Result<Self, &'static str> {
if data.len() != rows * cols {
return Err("The length of the Vec<f64> does not match the product of rows and cols");
}
Ok(Self {
rows,
cols,
data,
})
}
}