我正在尝试使用Rust中的分配器API,使用自定义分配器。
看起来Rust将Vec<u8,CustomAllocator>
和Vec<u8>
视为两种不同的类型。
let a: Vec<u8,CustomAllocator> = Vec::new_in(CustomAllocator);
for x in [1,2,3] { a.push(x) }
let b:Vec<u8> = vec![1,2,3];
assert_eq!(a,b);
这意味着像下面这样的简单比较将无法编译:
error[E0277]: can't compare `Vec<u8, CustomAllocator>` with `Vec<u8>`
--> src/main.rs:37:5
|
37 | assert_eq!(a,b);
| ^^^^^^^^^^^^^^^ no implementation for `Vec<u8, CustomAllocator> == Vec<u8>`
|
= help: the trait `PartialEq<Vec<u8>>` is not implemented for `Vec<u8, CustomAllocator>`
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
由于我没有拥有 Vec
或者 PartialEq
,因此无法实现这个特性。
实际上,在我的实现中,我可能比较两个底层的切片。但是我不知道该如何用这种语言来实现...
如果您有任何线索,请告诉我!
assert_eq!(&a[..], &b[..])
- PitaJassert_eq!(a[..], b[..])
。 - Chayim Friedman