将 u8 数组的引用转换为 <Vec<u8>>

Converting a reference of an array of u8 into a <Vec<u8>>

我正在尝试创建一个接受 T: Into<Vec<u8>> 的函数,但是当我尝试将 u8 的数组传递给它时,即使 From<&'a [T]>> 也无法编译由 Vec:

实现
the trait `std::convert::From<&[u8; 5]>` is not implemented for `std::vec::Vec<u8>`

这是我的代码

fn is_hello<T: Into<Vec<u8>>>(s: T) {
    let bytes = b"hello".to_vec();
    assert_eq!(bytes, s.into());
}

fn main() {
    is_hello(b"hello");
}

它不起作用,因为 b"hello" 有类型 &[u8; 5],它没有实现 Into<Vec<u8>>。您需要传递 &[u8] 切片才能编译:

is_hello(&b"hello"[..]);

我推荐以下问题来解释切片和数组之间的区别:

数组通常被强制切片,但有时没有隐式转换。

还有其他一些强制强制转换的方法:

b"hello" as &[u8]
b"hello".borrow()