本文介绍了如何为我自己的结构在 Vector 上实现 PartialEq?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下定义:

pub struct List<T> {
    memory:  Vec<T>,
}

对于这种类型,我将获得与 #[derive(PartialEq)] 等效的内容,例如 如何实现 PartialEq?

I would get the equivalent of #[derive(PartialEq)] for this type like describe in How can I implement PartialEq?

我使用匹配表达式,例如:

I use a match expression, like:

impl<T: PartialEq> PartialEq for List<T> {
    fn eq(&self, other: &List<T>) -> bool {
        self.memory == other.memory
    }
}
impl<T: fmt::Debug> fmt::Debug for List<T> where T:Display {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        try!(write!(f, "["));
        for (count, v) in self.memory.iter().enumerate() {
            if count != 0 { try!(write!(f, ", ")); }
            try!(write!(f, "{}", v));
        }
        write!(f, "]")
    }
}
impl<T> List<T> {
    pub fn new() -> Self {
        List {
            memory: Vec::new(),
        }
    }
    // push() add to end of list
    pub fn push(&mut self, value: T) {
        self.memory.push(value);
    }
}

但是编译器给了我这些错误:

But the compiler gives me these errors:

错误:类型不匹配 [E0308]

如果!( * left_val == * right_val ) {

if ! ( * left_val == * right_val ) {

注意:在这个 assert_eq 的扩展中!

note: in this expansion of assert_eq!

help:运行rustc --explain E0308查看详细说明

help: run rustc --explain E0308 to see a detailed explanation

注意:预期类型 librusty_data_structures::List

注意:找到类型[_;4]

产生编译错误的 main.rs

main.rs that produce compile errors

let mut listex: List<u32> = List::new();
listex.push(17);
listex.push(18);
listex.push(19);
listex.push(20);
assert_eq!(listex, [17, 18, 19, 20]);

我不明白为什么这很重要.为什么它甚至看着那种类型?

I don't understand why that matters. Why is it even looking at that type?

推荐答案

listex[17, 18, 19, 20] 有不同的类型(List<u32>[_; 4]) ,因此您无法检查它们是否相等.您需要更改 assert_eq!() 的参数之一的类型,以便类型匹配.最简单的选择是引用 listexmemory:

listex and [17, 18, 19, 20] have different types (List<u32> and [_; 4]) , so you cannot check for their equality. You need to change the type of one of the arguments of assert_eq!() so the types match. The simplest option would be to reference listex's memory:

assert_eq!(&listex.memory[0..4], [17, 18, 19, 20]);

或者您可以将 [17, 18, 19, 20] 转换为 List 以便 PartialEq 实现 List 可以付诸行动了.

Or you can convert [17, 18, 19, 20] to a List<u32> so that the PartialEq implementation for List<T> can be put into action.

如果您要将 listex 与另一个 List 进行比较,您的 PartialEq 实现将允许检查相等性(尽管您需要List 派生 Debug 以便对它们执行 assert_eq!()).

If you were to compare listex with another List<32>, your PartialEq implementation would allow checks for equality (though you would need the List<T> to derive Debug in order to do perform assert_eq!() on them).

编辑:至于你的问题为什么它甚至在看那种类型?",请注意在你的 PartialEq 实现中:

Edit: as for your question "Why is it even looking at that type?", notice that in your implementation of PartialEq:

fn eq(&self, other: &List)

您指定 eq 仅适用于 &List 类型的两个参数(&self 指向 列表).

You specify that eq works only for two arguments of type &List<T> (&self points to List<T>).

这篇关于如何为我自己的结构在 Vector 上实现 PartialEq?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:36