Intro

Not long ago I posed a challenge for those of us learning rust: https://lemmy.ml/post/12478167.

Basically write an equivalent of git diff --no-index A B … a file differ.

While it’s never too late to attempt it, I figured it’d be a good time to check in to see what anyone thought of it, in part because some people may have forgotten about it and would still like to have a shot, and also because I had a shot and am happy with what I wrote.

Check In

I’ll post where I got up to below (probably as a comment), but before that, does anyone have anything to share on where they got up to … any general thoughts on the challenge and the general idea of these?

My experience

My personal experience was that I’d not kept up with my rust “studies” for a bit and used this as a good “warm up” or “restart” exercise and it worked really well. Obviously learning through doing is a good idea, and the Rust Book is a bit too light, IMO, on good exercises or similar activities. But I found this challenge just difficult enough to make me feel more comfortable with the language.

Future Challenges

Any ideas for future challenges??

My quick thoughts

  • A simple web app like a todo app using axtix_web and diesel and some templating crate.
  • Extend my diffing program to output JSON/HTML and then to diff by characters in a string
  • A markdown parser??
  • Jayjader@jlai.luM
    link
    fedilink
    English
    arrow-up
    2
    ·
    8 months ago

    I also found using an enum for the state variable along with associating data with each state a useful, flexible and elegant way to go. The program basically becomes just a single enum and the looping pattern above, along with a few data structures and some methods. Is this normal/idiomatic for rust??

    In my experience, this is the sweet spot for Rust programming. If you can formulate your approach as a state machine like this, then you almost always should - especially when writing in Rust.

    The only times I’d pass on the pattern is if stuffing all of the ambiant state of a problem into different states of a state machine just to make the giant loop work correctly introduces too much cognitive complexity (aka makes the code too hard to read / follow).

    • maegul (he/they)OPM
      link
      fedilink
      English
      arrow-up
      1
      ·
      8 months ago

      Thanks for the feedback! What you say tracks exactly with my feelings after writing this.

      I personally did encounter enough issues with the borrow checker while writing this, largely because I’m not on top of ownership and language enough, that I’m still somewhat wary of moving off of clean data flow patterns like this state machine one.

      Maybe I should try re-writing it using “dumb” globals and a procedural/imperative style and see how I go?

      What would you be reaching for, in terms of patterns/approaches, when you know your data flow is too messy for a state machine?

      • Jayjader@jlai.luM
        link
        fedilink
        English
        arrow-up
        2
        ·
        8 months ago

        What would you be reaching for, in terms of patterns/approaches, when you know your data flow is too messy for a state machine?

        Bare if and loop expressions. Basically what you described as “dumb globals and a procedural/impérative style”. Of course, defining some custom types is almost always useful if not needed for “properly” handling ownership.