String equality in Rust: how does referencing and dereferencing work?

送分小仙女□ 提交于 2020-01-30 04:43:29

问题


As a Rust newbie, I'm working through the Project Euler problems to help me get a feel for the language. Problem 4 deals with palindromes, and I found two solutions for creating a vector of palindromes, but I'm not sure how either of them work.

I'm using a vector of strings, products, that's calculated like this:

let mut products = Vec::new();
for i in 100..500 {
    for j in 500..1000 {
        products.push((i * j).to_string());
    }
}

For filtering these products to only those that are palindromic, I have the following two solutions:

Solution 1:

let palindromes: Vec<_> = products
    .iter()
    .filter(|&x| x == &x.chars().rev().collect::<String>())
    .collect();

Solution 2:

let palindromes: Vec<_> = products
    .iter()
    .filter(|&x| *x == *x.chars().rev().collect::<String>())
    .collect();

They both yield the correct result, but I have no idea why!

In Solution 1, we're comparing a reference of a string to a reference of a string we've just created?

In Solution 2, we dereference a reference to a string and compare it to a dereferenced new string?

What I would expect to be able to do:

let palindromes: Vec<_> = products
    .iter()
    .filter(|x| x == x.chars().rev().collect::<String>())
    .collect();

I'm hoping somebody will be able to explain to me:

  • What is the difference is between my two solutions, and why do they both work?
  • Why can't I just use x without referencing or dereferencing it in my filter function?

Thank you!


回答1:


  1. Vec<String>.iter() returns an iterator over references (&String).
  2. The closure argument of .filter() takes a reference to an iterator's item. So the type that is passed to the closure is a double reference &&String.
  3. |&x| tells the closure to expect a reference, so x is now of type &String.

First solution: collect returns a String, of which & takes the reference. x is also a reference to a string, so the comparison is between two &String.

Second solution: The dereference operator * is applied to x, which results in a String. The right hand side is interesting: The String result of collect is dereferenced. This results in a string slice because String implements Deref<Target=str>. Now the comparison is between String and str, which is works because it is implemented in the standard library (Note that a == b is equivalent to a.eq(&b)).

Third solution: The compiler explains why it does not work.

the trait std::cmp::PartialEq<std::string::String> is not implemented for &&std::string::String

The left side is a double reference to string (&&String) and the right side is just a String . You need to get both sides to the same "reference level". All of these work:

x.iter().filter(|x| x == &&x.chars().rev().collect::<String>());
x.iter().filter(|x| *x == &x.chars().rev().collect::<String>());
x.iter().filter(|x| **x == x.chars().rev().collect::<String>());


来源:https://stackoverflow.com/questions/50133735/string-equality-in-rust-how-does-referencing-and-dereferencing-work

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