title: String equality in Rust: how does referencing and dereferencing work?
body: <p>As a Rust newbie, I'm working through the <a href="https://projecteuler.net/" rel="noreferrer">Project Euler</a> 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.</p>
  
  <p>I'm using a vector of strings, <code>products</code>, that's calculated like this:</p>
  
  <pre><code>let mut products = Vec::new();
  for i in 100..500 {
      for j in 500..1000 {
          products.push((i * j).to_string());
      }
  }
  </code></pre>
  
  <p>For filtering these products to only those that are palindromic, I have the following two solutions:</p>
  
  <p><strong>Solution 1:</strong></p>
  
  <pre><code>let palindromes: Vec&lt;_&gt; = products
      .iter()
      .filter(|&amp;x| x == &amp;x.chars().rev().collect::&lt;String&gt;())
      .collect();
  </code></pre>
  
  <p><strong>Solution 2:</strong></p>
  
  <pre><code>let palindromes: Vec&lt;_&gt; = products
      .iter()
      .filter(|&amp;x| *x == *x.chars().rev().collect::&lt;String&gt;())
      .collect();
  </code></pre>
  
  <p>They both yield the correct result, but I have no idea why!</p>
  
  <p>In Solution 1, we're comparing a reference of a string to a reference of a string we've just created?</p>
  
  <p>In Solution 2, we dereference a reference to a string and compare it to a dereferenced new string?</p>
  
  <p><strong>What I would expect to be able to do:</strong></p>
  
  <pre><code>let palindromes: Vec&lt;_&gt; = products
      .iter()
      .filter(|x| x == x.chars().rev().collect::&lt;String&gt;())
      .collect();
  </code></pre>
  
  <p>I'm hoping somebody will be able to explain to me:</p>
  
  <ul>
  <li>What is the difference is between my two solutions, and why do they both work?</li>
  <li>Why can't I just use <code>x</code> without referencing or dereferencing it in my filter function?</li>
  </ul>
  
  <p>Thank you!</p>