【发布时间】:2017-11-14 02:32:56
【问题描述】:
如何在 Rust 中比较数组切片和向量?有问题的代码:
fn parse<R: io::Read>(reader: R, fixed: &[u8]) -> io::Result<bool> {
let mut buf = vec![0; fixed.len()];
match reader.read(&mut buf) {
Ok(n) => Ok(n == fixed.len() && fixed == &mut buf),
Err(e) => Err(e)
}
}
我得到的错误:
error[E0277]: the trait bound `[u8]: std::cmp::PartialEq<std::vec::Vec<u8>>` is not satisfied
--> src/main.rs:32:47
|
32 | Ok(n) => Ok(n == fixed.len() && fixed == &mut buf),
| ^^ can't compare `[u8]` with `std::vec::Vec<u8>`
|
= help: the trait `std::cmp::PartialEq<std::vec::Vec<u8>>` is not implemented for `[u8]`
答案一定很简单,但它让我难以捉摸。
【问题讨论】:
标签: rust