I'm trying to make a function that will return an iterator of all the files in a directory, including all the files in the subdirectories. As I don't know the size of an array that would contain all the files' paths, I thought that it would be easier to have the function return an iterator instead of an array. It's simple enough to do in Python:
def func():
for i in range(0, 100):
yield i
for i in func():
print(i)
But when I try to do something similar in Rust, I get compiler errors and/or compiler panics. Here, I tried some basic syntax that's close to what it is in Python:
fn func() -> Iterator {
for i in 0..100 {
yield i;
}
}
fn main() {
for i in func() {
println!("{}", i);
}
}
But when I compiled it, it caused two errors and a warning:
error[E0658]: yield syntax is experimental
--> src/main.rs:3:9
|
3 | yield i;
| ^^^^^^^
|
= note: see issue #43122 <https://github.com/rust-lang/rust/issues/43122> for more information
warning: trait objects without an explicit `dyn` are deprecated
--> src/main.rs:1:14
|
1 | fn func() -> Iterator {
| ^^^^^^^^ help: use `dyn`: `dyn Iterator`
|
= note: `#[warn(bare_trait_objects)]` on by default
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
= note: for more information, see issue #80165 <https://github.com/rust-lang/rust/issues/80165>
error[E0191]: the value of the associated type `Item` (from trait `Iterator`) must be specified
--> src/main.rs:1:14
|
1 | fn func() -> Iterator {
| ^^^^^^^^ help: specify the associated type: `Iterator<Item = Type>`
Some errors have detailed explanations: E0191, E0658.
For more information about an error, try `rustc --explain E0191`.
warning: `problem` (bin "problem") generated 1 warning
error: could not compile `problem` due to 2 previous errors; 1 warning emitted
I've been playing around with using different return types, like dyn Iterator<Item = i32>
, impl Iterator
, etc. according to the help in the error messages, and I either get errors, compiler panics, or both. Sorry if this is a stupid question; I've only been working with Rust for around three months. But somehow, it feels like this should be simpler.
So my question: what's the correct syntax for a function returning an iterator generated using the yield
keyword? I've looked in the Rust Documentation and The Book, but I've found nothing useful.