似乎有两种方法可以将向量转换为数组,一种是通过切片(fn a
),另一种是直接转换(fn b
):
use std::array::TryFromSliceError;
use std::convert::TryInto;
type Input = Vec<u8>;
type Output = [u8; 1000];
// Rust 1.47
pub fn a(vec: Input) -> Result<Output, TryFromSliceError> {
vec.as_slice().try_into()
}
// Rust 1.48
pub fn b(vec: Input) -> Result<Output, Input> {
vec.try_into()
}
实际上,它们之间有什么区别?只是错误类型吗?后者被添加的事实让我想知道是否有更多原因。
Vec
到数组的实现,在失败时它会将输入数组作为错误返回。 - harmic