gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:26:19 25 4
gpt4 key购买 nike

<分区>

我有以下三个例子:

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`

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com