title: How to pass multiple args to a rust closure - expected closure that takes 1 argument
body: <p>I am new to closures and I don't understand why I am getting the following error.</p>
  
  <p>If I don't include the second argument its works:</p>
  
  <pre><code>fn obj_x(x: Vec&lt;f64&gt;) -&gt; f64 {
      return 0.0;
  };
  
  let f = |x: &amp;Vec&lt;f64&gt;| obj_x(x.to_vec());
  </code></pre>
  
  <p>Here is my closure:</p>
  
  <pre><code>fn foo() {
      let f = |x: &amp;Vec&lt;f64&gt;, N: usize| obj_x(x.to_vec(), N);
  }
  
  fn obj_x(x: Vec&lt;f64&gt;, N: usize) -&gt; f64 {
      let x = (x[0] + 4.0).powf(2.0);
  
      return x;
  }
  
  </code></pre>
  
  <p>But unfortunately it fails</p>
  
  <pre class="lang-none prettyprint-override"><code>error[E0593]: closure is expected to take 1 argument, but it takes 2 arguments
     --&gt; src/main.rs:120:44
      |
  114 |     let f = |x: &amp;Vec&lt;f64&gt;, N: usize | obj_x(x.to_vec(),N);
      |             ------------------------- takes 2 arguments
  ...
  120 |         let mut fmin = Funcmin::new(&amp;mut x,&amp;f,&amp;g,"cg");
      |                                            ^^ expected closure that takes 1 argument
      |
  
  </code></pre>
  
  <p>Here is my full code:</p>
  
  <pre><code>fn obj_x(x: Vec&lt;f64&gt;, N: usize) -&gt; f64 {
      let x = (x[0] + 4.0).powf(2.0);
  
      return x;
  }
  
  fn gradient_x(x: Vec&lt;f64&gt;) -&gt; Vec&lt;f64&gt; {
      return vec![2.0 * (x[0] + 4.0)];
  }
  
  fn test() {
      let f = |x: &amp;Vec&lt;f64&gt;, &amp;N: usize| obj_x(x.to_vec(), N);
      let g = |x: &amp;Vec&lt;f64&gt;| gradient_x(x.to_vec());
      let mut x = vec![40.0f64];
      let mut N = 2220;
      {
          //you must create a mutable object
          let mut fmin = Funcmin::new(&amp;mut x, &amp;f, &amp;g, "cg");
          fmin.minimize();
      }
      println!("{:?}", x);
  }
  </code></pre>