title: How to return an iterator from a function and use it?
body: <p>I'm trying to teach myself Rust by working on a little calendar project.</p>
  
  <p>Here I'm trying to generate a list of dates spanning three whole months around a given date.
  I'd like to return an iterator that can iterate over these dates.
  Here's my first attempt:</p>
  
  <pre><code>fn three_months_range(tm: time::Tm) -&gt; std::iter::Iterator&lt;Item=time::Tm&gt; {
      let fpm: time::Tm = first_of_previous_month(&amp;tm);
      (0..)
          .map(|i| fpm + time::Duration::days(i))
          .take_while(|&amp;t| t.tm_mon != (tm.tm_mon + 2) % 12)
  }
  </code></pre>
  
  <p>Unfortunately, this doesn't compile and I get an error.</p>
  
  <pre><code>src/main.rs:49:40: 49:75 error: the trait `core::marker::Sized` is not implemented for the type `core::iter::Iterator&lt;Item=time::Tm&gt; + 'static` [E0277]
  src/main.rs:49 fn three_months_range(tm: time::Tm) -&gt; std::iter::Iterator &lt;Item=time::Tm&gt; {
                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/main.rs:49:40: 49:75 help: run `rustc --explain E0277` to see a detailed explanation
  src/main.rs:49:40: 49:75 note: `core::iter::Iterator&lt;Item=time::Tm&gt; + 'static` does not have a constant size known at compile-time
  src/main.rs:49:40: 49:75 note: the return type of a function must have a statically known size
  </code></pre>
  
  <p>"the return type of a function must have a statically known size". Ok, after some research, it seems that the solution is to return the iterator through a Box.
  (still, I'm left wondering how the standard library <code>map</code>, <code>filter</code>, <code>take_while</code>... methods manage to return Iterators and not boxes).</p>
  
  <p>Well, here's the second attempt, which compiles successfully:</p>
  
  <pre><code>fn three_months_range(tm: time::Tm) -&gt; Box&lt;iter::Iterator&lt;Item=time::Tm&gt;&gt; {
      let fpm: time::Tm = first_of_previous_month(&amp;tm);
      Box::new(
          (0..)
          .map(move |i| fpm + time::Duration::days(i))
          .take_while(move |&amp;t| t.tm_mon != (tm.tm_mon + 2) % 12)
      )
  }
  </code></pre>
  
  <p>Unfortunately, I don't manage to use this iterator.
  For example, let's say I want to build a vector containing the days of the months of each date (1, 2, 3, ..., 31, 1, 2, ..., 30, 1, 2, ... 31):</p>
  
  <pre><code>let days_vec: Vec&lt;u64&gt; = 
      ( *three_months_range(time::now_utc()) )
      .map( |&amp;t: &amp;time::Tm| t.tm_mday )
      .collect();
  </code></pre>
  
  <p>→</p>
  
  <pre><code>src/main.rs:14:10: 14:42 error: the trait `core::marker::Sized` is not implemented for the type `core::iter::Iterator&lt;Item=time::Tm&gt; + 'static` [E0277]
  src/main.rs:14         .map( |&amp;t: &amp;time::Tm| t.tm_mday )
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/main.rs:14:10: 14:42 help: run `rustc --explain E0277` to see a detailed explanation
  src/main.rs:14:10: 14:42 note: `core::iter::Iterator&lt;Item=time::Tm&gt; + 'static` does not have a constant size known at compile-time
  src/main.rs:15:10: 15:19 error: no method named `collect` found for type `core::iter::Map&lt;core::iter::Iterator&lt;Item=time::Tm&gt; + 'static, [closure@src/main.rs:14:15: 14:40]&gt;` in the current scope
  src/main.rs:15         .collect();
                          ^~~~~~~~~
  src/main.rs:15:10: 15:19 note: the method `collect` exists but the following trait bounds were not satisfied: `core::iter::Iterator&lt;Item=time::Tm&gt; : core::marker::Sized`, `[closure@src/main.rs:14:15: 14:40] : core::ops::FnMut&lt;(time::Tm,)&gt;`, `core::iter::Map&lt;core::iter::Iterator&lt;Item=time::Tm&gt; + 'static, [closure@src/main.rs:14:15: 14:40]&gt; : core::iter::Iterator`
  src/main.rs:14:10: 14:42 error: type mismatch: the type `[closure@src/main.rs:14:15: 14:40]` implements the trait `for&lt;'r&gt; core::ops::FnMut&lt;(&amp;'r time::Tm,)&gt;`, but the trait `core::ops::FnMut&lt;(time::Tm,)&gt;` is required (expected struct `time::Tm`, found &amp;-ptr) [E0281]
  src/main.rs:14         .map( |&amp;t: &amp;time::Tm| t.tm_mday )
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/main.rs:14:10: 14:42 help: run `rustc --explain E0281` to see a detailed explanation
  src/main.rs:14:10: 14:42 error: type mismatch: the type `[closure@src/main.rs:14:15: 14:40]` implements the trait `for&lt;'r&gt; core::ops::FnOnce&lt;(&amp;'r time::Tm,)&gt;`, but the trait `core::ops::FnOnce&lt;(time::Tm,)&gt;` is required (expected struct `time::Tm`, found &amp;-ptr) [E0281]
  src/main.rs:14         .map( |&amp;t: &amp;time::Tm| t.tm_mday )
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  src/main.rs:14:10: 14:42 help: run `rustc --explain E0281` to see a detailed explanation
  error: aborting due to 4 previous errors
  </code></pre>
  
  <p>That's a lot of errors.</p>
  
  <p>So what am I doing wrong here?</p>
  
  <p>Is there a relatively simple way to transform iterators in Rust and/or return them from functions?</p>