other-languages

here be heresies and things we have to use for work
borkdude 2019-12-03T12:28:29.018500Z

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();
}

borkdude 2019-12-03T12:28:43.018800Z

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 `()
`

borkdude 2019-12-03T12:30:59.019300Z

I get the error, but I don't know how to fix it yet

borkdude 2019-12-03T12:33:56.019500Z

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();
}

1
gklijs 2019-12-03T15:18:42.021500Z

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.

borkdude 2019-12-03T15:25:09.021900Z

@gklijs This came directly from a Rust readme

gklijs 2019-12-03T15:27:16.023100Z

Well, it does work, but seems weird to me.

borkdude 2019-12-03T16:04:18.023400Z

@gklijs it came from here: https://rust-lang-nursery.github.io/rust-cookbook/file/dir.html

gklijs 2019-12-03T16:08:02.024300Z

Yes, you should probable want to do something with the error in main trough, and not just ignore the result.

borkdude 2019-12-03T16:10:12.024600Z

first silence the type checker .. oh that's not the point is it? 😉