how do I make this compile in Rust?
use walkdir::WalkDir;
fn print() {
for entry in WalkDir::new("foo") {
println!("{}", entry?.path().display());
};
}
fn main() {
print();
}
error[E0277]: the `?` operator can only be used in a function that returns `Result` or `Option` (or another type that implements `std::ops::Try`)
--> src/main.rs:5:24
|
5 | println!("{}", entry?.path().display());
| ^^^^^^ cannot use the `?` operator in a function that returns `()
`I get the error, but I don't know how to fix it yet
This worked!
use walkdir::WalkDir;
use std::io;
fn print() -> Result<(), io::Error> {
for entry in WalkDir::new(".") {
println!("{}", entry?.path().display());
};
return Ok(());
}
fn main() {
let _ = print();
}
It a bit rusty, but I think instead use entry.expect("empty entry").path.display()
might be better, Don't make much sense to return an error and do nothing with it, but it probably will not be called anyway.
@gklijs This came directly from a Rust readme
Well, it does work, but seems weird to me.
@gklijs it came from here: https://rust-lang-nursery.github.io/rust-cookbook/file/dir.html
Yes, you should probable want to do something with the error in main trough, and not just ignore the result.
first silence the type checker .. oh that's not the point is it? 😉