Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Very unfamiliar with Rust, very familiar with C++. As a reader of this Rust program, how can I figure out what's happening? I don't see anything in GlobWalker documentation that even slightly implies that the filter_map calls that follow are appropriate. Also, why the first filter_map(Result::Ok)? Implies the iterator emits Result? Hard to follow, to be honest.


So, this[0] is the doc page for the `GlobWalker` type. One of the traits it implements, and the only one relevant here, is the `Iterator` trait from the standard library[1]. That's how you'd know that the `filter_map` function is appropriate.

The implementation there also tells you that the `Item` type of the iterator is a `Result<DirEntry, WalkError>`, presumably because any of the file system reads can fail. The `WalkError` type is from the globwalk library.

Going back to `filter_map`[2] that function takes a function or closure that maps from `Item` to the `Option` enum, and then just discards anything that is the `None` variant of the enum, filtering them out. `Result::ok` is a function on `Result` that converts the `Result` to an `Option`, converting `Err` to a `None`. So this is discarding the errors without reporting them.

As someone who is familiar with Rust, this wasn't particularly hard to follow, but that comes down to familiarity with these adaptor functions and how they can be plugged into each other.

[0] https://docs.rs/globwalk/0.8.0/globwalk/struct.GlobWalker.ht... [1] https://doc.rust-lang.org/stable/std/iter/trait.Iterator.htm... [2] https://doc.rust-lang.org/stable/std/iter/trait.Iterator.htm...


Aha, I didn't notice the little [+] widget next to the Iterator trait in the docs. You have to click it to get the Item type, otherwise it's hidden. Thanks!


You're welcome. The implementation in the OP uses a more functional style, but that's not the only way Rust is commonly written. I also did an implementation[0] (because I have nothing better to do) using more of an imperative style, which you may be more familiar with. Admittedly I did go a bit overkill on string normalization and avoiding allocations while doing so.

It also includes a test case which the OP's implementation will get wrong because the order of the combining diacritics is different in both words. If you don't normalize them you'll get two different words.

[0] https://gist.github.com/Measter/e2e287ee21311d34ea8eb8cd9d57...


IMO, globwalk is inappropriate to use here. It's bringing in a ton of unnecessary dependencies for such a simple task. Just using walkdir directly (which globwalk depends on) and filtering on the file extension would be just nearly as simple and use oodles less code.

This is IMO a good example of poor choices that an "npm-like ecosystem" can encourage. Obviously there are deep and fundamental trade offs here, but other than watching crates compile, you're almost completely removed from just how much code you're bringing in relative to what is really necessary.


Thanks - I appreciate this feedback. I've updated the post with a modified version at the end that directly uses walkdir (and I agree with you).

To the GP, the place it shows up in the docs is mostly in its example: if let Ok(img) = img <-- this is a sign they're unpacking a Result type.


Awesome! And globwalk is a great crate, when you really do need high performance glob walking. I think there is room to improve it so that it doesn't use so much code, but it's a bit of work to do that. (I see this as largely my failure, as the 'ignore' crate is primarily to blame here.)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: