Rust:如何从函数返回迭代器并使用它?

时间:2016-04-03 12:44:42

标签: iterator closures rust

我试图通过一个小日历项目来教自己Rust。

我试图在给定日期生成一个跨越整整三个月的日期列表。 我想返回一个可以迭代这些日期的迭代器。 这是我的第一次尝试:

fn three_months_range(tm: time::Tm) -> std::iter::Iterator<Item=time::Tm> {
    let fpm: time::Tm = first_of_previous_month(&tm);
    (0..)
        .map(|i| fpm + time::Duration::days(i))
        .take_while(|&t| t.tm_mon != (tm.tm_mon + 2) % 12)
}

不幸的是,这并没有编译,我收到了错误。

src/main.rs:49:40: 49:75 error: the trait `core::marker::Sized` is not implemented for the type `core::iter::Iterator<Item=time::Tm> + 'static` [E0277]
src/main.rs:49 fn three_months_range(tm: time::Tm) -> std::iter::Iterator <Item=time::Tm> {
                                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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<Item=time::Tm> + '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

&#34;函数的返回类型必须具有静态已知的大小&#34;。好吧,经过一些研究,似乎解决方案是通过Box返回迭代器。 (不过,我还是想知道标准库mapfiltertake_while ...方法如何设法返回迭代器而不是框。)

嗯,这是第二次成功编译的尝试:

fn three_months_range(tm: time::Tm) -> Box<iter::Iterator<Item=time::Tm>> {
    let fpm: time::Tm = first_of_previous_month(&tm);
    Box::new(
        (0..)
        .map(move |i| fpm + time::Duration::days(i))
        .take_while(move |&t| t.tm_mon != (tm.tm_mon + 2) % 12)
    )
}

不幸的是,我没有设法使用这个迭代器。 例如,让我们说我想构建一个包含每个日期月份日期的向量(1,2,3,...,31,1,2,...,30,1,2) ,... ... 31):

let days_vec: Vec<u64> = 
    ( *three_months_range(time::now_utc()) )
    .map( |&t: &time::Tm| t.tm_mday )
    .collect();

src/main.rs:14:10: 14:42 error: the trait `core::marker::Sized` is not implemented for the type `core::iter::Iterator<Item=time::Tm> + 'static` [E0277]
src/main.rs:14         .map( |&t: &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<Item=time::Tm> + '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<core::iter::Iterator<Item=time::Tm> + 'static, [closure@src/main.rs:14:15: 14:40]>` 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<Item=time::Tm> : core::marker::Sized`, `[closure@src/main.rs:14:15: 14:40] : core::ops::FnMut<(time::Tm,)>`, `core::iter::Map<core::iter::Iterator<Item=time::Tm> + 'static, [closure@src/main.rs:14:15: 14:40]> : 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<'r> core::ops::FnMut<(&'r time::Tm,)>`, but the trait `core::ops::FnMut<(time::Tm,)>` is required (expected struct `time::Tm`, found &-ptr) [E0281]
src/main.rs:14         .map( |&t: &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<'r> core::ops::FnOnce<(&'r time::Tm,)>`, but the trait `core::ops::FnOnce<(time::Tm,)>` is required (expected struct `time::Tm`, found &-ptr) [E0281]
src/main.rs:14         .map( |&t: &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

这是很多错误。

那我在这里做错了什么?

是否有一种相对简单的方法来转换Rust中的迭代器和/或从函数返回它们?

1 个答案:

答案 0 :(得分:3)

问题是你试图从Iterator中取出Box(你不能,因为它是一个特征对象而不是Sized)。但Box是透明的,您可以直接使用map

let days_vec: Vec<u64> = 
    three_months_range(time::now_utc())
    .map( |&t: &time::Tm| t.tm_mday )
    .collect();

请注意,map函数希望您按值而不是通过引用获取参数。所以这个电话看起来像这样:

.map(|t| t.tm_mday)