News for hackers. Stuff that matters.
Re:A half-hour to learn Rust by projct on Saturday January 02, 2021 @05:23PM (#25617138)
If you get rid of unwrap and use `if let` and `match`, this code will clean up nicely.

If the code is supposed to be maintainable, you could also make something like a FromRegex trait for Input. It would be a good idea to try exercism's mentoring thing to get better at writing it the easier way the first time. The mentoring thing really is what helped me to think in ways that made this mess easier to avoid.

Re:A half-hour to learn Rust by mikepurvis on Saturday January 02, 2021 @06:35PM (#25617735)
Fair fair. To be honest, I think in a lot of these cases there are good helper crates/macros; for this I would now probably use recap: https://docs.rs/recap/0.1.1/recap/

The really horrendous scenario was when I was trying to navigate pest iterators for parsing according to a grammar.

Re:A half-hour to learn Rust by kochismo on Saturday January 02, 2021 @11:16PM (#25619374)
Even without if let or match many of those unwraps are unnecessary. See https://github.com/zookini/aoc-2020/blob/master/src/bin/2.rs
Re:A half-hour to learn Rust by mikepurvis on Saturday January 02, 2021 @11:36PM (#25619491)
Interesting! Can you help me understand why this doesn't work for me?

    error[E0599]: no method named `parse` found for enum `Option<regex::Match<'_>>` in the current scope
      --> src\main.rs:24:39
       |
    24 |                 min: caps.name("min").parse().unwrap(),
       |                                       ^^^^^ method not found in `Option<regex::Match<'_>>`
Re:A half-hour to learn Rust by abhijat on Sunday January 03, 2021 @04:14AM (#25620650)
I think you need .name("min")?.as_str() to access the underlying text of the match object (after making sure it is a valid match with the ?), which can then be parsed. The regex Match object itself does not have a parse method that I can see.

I don't know what the structure Input looks like but I played around with your code and it seems to work with as_str()

https://play.rust-lang.org/?version=stable&mode=debug&editio...

Re:A half-hour to learn Rust by kochismo on Sunday January 03, 2021 @08:07AM (#25621550)
caps.name("min") returns an Option which you need to handle e.g. by unwrapping. You can use caps["min"] instead if you don't want an option.