I have three following examples:
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);
}
}
When compiling the iterate_with_deref_boxed_iterator
the following error appears:
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`
Why does dereferencing break compilation?