为什么不可能取消引用装箱的迭代器特征对象?

时间:2019-06-25 15:02:33

标签: rust iterator

我有以下三个示例:

fn iterate_with_iterator<T: std::fmt::Display, I: Iterator<Item = T>>(iter: I) {
    for x in iter {
         println!("{}", x);
    }
}

fn iterate_with_boxed_iterator<'a, T: std::fmt::Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
    for x in iter {
         println!("{}", x);
    }
}

fn iterate_with_deref_boxed_iterator<'a, T: std::fmt::Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
    let diter = *iter;
    for x in diter {
         println!("{}", x);
    }
}

Playground

编译iterate_with_deref_boxed_iterator时出现以下错误:

error[E0277]: the size for values of type `dyn std::iter::Iterator<Item = T>` cannot be known at compilation time
  --> src/main.rs:14:9
   |
14 |     let diter = *iter;
   |         ^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::iter::Iterator<Item = T>`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: all local variables must have a statically known size
   = help: unsized locals are gated as an unstable feature

error[E0277]: the size for values of type `dyn std::iter::Iterator<Item = T>` cannot be known at compilation time
  --> src/main.rs:15:14
   |
15 |     for x in diter {
   |              ^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `dyn std::iter::Iterator<Item = T>`
   = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
   = note: required by `std::iter::IntoIterator::into_iter`

为什么取消引用中断编译?

1 个答案:

答案 0 :(得分:2)

无法将任何特征对象(类型的dyn Iterator部分)从指针(&dyn IteratorBox<dyn Iterator>)的后面移出。这是因为特征对象是未调整大小的–编译器没有有关所引用对象具体大小的信息,因此不允许将其移入必须在编译时知道其大小的堆栈中。这就是您的错误消息(“ std::marker::Sized未实现特征dyn std::iter::Iterator<Item = T>”)的意思。

Rust目前不支持运行时变量堆栈分配,因此不允许将未确定大小的值(如特征对象)移入堆栈。

如果您确实需要拆开迭代器的框,则可以创建一个通用函数

fn deref_boxed<I: Iterator<Item = T>, T: Display>(iter: Box<I>) {
    let mut i = *iter;
    for item in i {
        println!("{}", item);
    }
}

但是您应该能够遍历该参数而无需将其拆箱:

fn iter_boxed<'a, T: Display>(iter: Box<dyn Iterator<Item = T> + 'a>) {
    for item in iter {
        println!("{}", x);
    }
}