How to implement PartialEq on Vector for my own structs?

喜欢而已 提交于 2019-12-24 08:25:39

问题


I have the following definition:

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

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:

error: mismatched types [E0308]

if ! ( * left_val == * right_val ) {

note: in this expansion of assert_eq!

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

note: expected type librusty_data_structures::List<u32>

note: found type [_; 4]

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?


回答1:


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]);

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.

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).

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<T>)

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




回答2:


Here is the solution, following cuviper response on The Rust Programming Language Forum

impl<T, U> PartialEq<U> for List<T>
    where Vec<T>: PartialEq<U>
{
    fn eq(&self, other: &U) -> bool {
        self.memory.eq(other)
    }
}

To test :

let mut listex: List<u32> = List::new();

listex.push(17);
listex.push(18);
listex.push(19);
listex.push(20);            

println!("{}", listex == [17, 18, 19, 20]);


来源:https://stackoverflow.com/questions/39265755/how-to-implement-partialeq-on-vector-for-my-own-structs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!