给定一个 i32
的二维向量:
let v = vec![
vec![1, 1, 1],
vec![0, 1, 0],
vec![0, 1, 0],
];
我如何将它传递给函数以最终打印其详细信息?我试过了:
fn printVector(vector: &[[i32]]) {
println!("Length{}", vector.len())
}
最佳答案
您可以使用接受 T
切片的函数,其中 T
也可以作为切片引用:
fn print_vector<T>(value: &[T])
where
T: AsRef<[i32]>,
{
for slice in value {
println!("{:?}", slice.as_ref())
}
}
如果你想接受任何类型而不仅仅是 i32
,你也可以概括这个:
fn print_vector<T, D>(value: &[T])
where
T: AsRef<[D]>,
D: Debug,
{
for slice in value {
println!("{:?}", slice.as_ref())
}
}
关于vector - 如何将 Vec<Vec<i32>> 传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51859222/