Mat*_*att 3 arrays generics traits rust
我正在尝试创建一个带有 的函数T: Into<Vec<u8>>
,但是当我尝试将 的数组传递u8
给它时,即使From<&'a [T]>>
由Vec
以下实现,它也不会编译:
the trait `std::convert::From<&[u8; 5]>` is not implemented for `std::vec::Vec<u8>`
Run Code Online (Sandbox Code Playgroud)
这是我的代码
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");
}
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为b"hello"
有 type &[u8; 5]
,它没有实现Into<Vec<u8>>
. 您需要传递一个&[u8]
切片才能编译:
is_hello(&b"hello"[..]);
Run Code Online (Sandbox Code Playgroud)
我推荐以下问题来解释切片和数组之间的区别:切片和数组之间的区别是什么?.