title: Creating Diesel.rs queries with a dynamic number of .and()'s
body: <p>While playing with Diesel, I got stuck writing a function which takes an vector of <code>String</code>s as input and does the following:</p>
  
  <ol>
  <li>Combine all <code>String</code>s to a large query</li>
  <li>run the query on the <code>Connection</code></li>
  <li>process the result</li>
  <li>return a <code>Vec</code></li>
  </ol>
  
  <p>If I construct the query in one step, it works just fine:</p>
  
  <pre><code>fn get_books(authors: Vec&lt;String&gt;, connection: SqliteConnection) {
      use schema::ebook::dsl::*;
  
      let inner = author
          .like(format!("%{}%", authors[0]))
          .and(author.like(format!("%{}%", authors[1])))
          .and(author.like(format!("%{}%", authors[2])));
  
      ebook
          .filter(inner)
          .load::&lt;Ebook&gt;(&amp;connection)
          .expect("Error loading ebook");
  }
  </code></pre>
  
  <p>If I try to generate the query in more steps (needed in order to work with  the variable length of the input vector) I can't get it to compile:</p>
  
  <pre><code>fn get_books(authors: Vec&lt;String&gt;, connection: SqliteConnection) {
      use schema::ebook::dsl::*;
  
      let mut inner = author
          .like(format!("%{}%", authors[0]))
          .and(author.like(format!("%{}%", authors[1]))); // &lt;1&gt;
  
      inner = inner.and(author.like(format!("%{}%", authors[2]))); // &lt;2&gt;
  
      ebook
          .filter(inner)
          .load::&lt;Ebook&gt;(&amp;connection)
          .expect("Error loading ebook");
  }
  </code></pre>
  
  <p>This generates the following error:</p>
  
  <pre class="lang-none prettyprint-override"><code>inner = inner.and(author.like(format!("%{}%",authors[2])));
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `diesel::expression::operators::Like`, found struct `diesel::expression::operators::And`
  </code></pre>
  
  <p>I don't understand why Rust expects a <code>Like</code> and not an <code>And</code>. The function an the line line marked <code>&lt;1&gt;</code> returns an <code>And</code> and therefore an <code>And</code> is stored in <code>inner</code>. </p>
  
  <p>What am I missing? Why does the first bit of code compile and the second won't? What is the right way to generate this kind of query?</p>