Intro

So … as we learn rust, exercises and puzzles are obviously valuable (at least in the early stages).

But IMO, jumping into just trying to write actually functional and useful programs can be very valuable.

I’ve been away for a bit and was wondering what I should do to start up rust learning again and figured that trying to power through some simple but not trivial program would be great. Then I figured people learning rust here might find the process worthwhile too.

The Idea of the Challenge

  • Pose a challenge in a post, outlining roughly what sort of program should be written, and at roughly what level it’s pitched.
  • Propose a time period during which people can try to write the program, while having as much of a discussion as they like about it of course
  • When the time period is up, make another post inviting everyone to post where they got up to, posting code so we can discuss and provide feedback.

Any thoughts on this?

If you’ve got ideas for other challenges, feel free to post them. If there are a number of ideas it might be best to catalogue them and run them every week or month so as not to clog things.

The Challenge

A file diff program. Basically something like git diff or diff/colordiff.

git diff --no-index A B
  • Takes two files/paths as inputs, “A” and “B”.
  • Determines the differences between them, or rather, what changes have been made to “B” relative to “A”.
  • Changes are measured on a line by line bases (generally like git).
  • Provides some sort of output showing these changes.

Discussion

As I see it (not being an expert in this sort of program), there are three kinds of changes:

  • Addition
  • Deletion
  • Alteration

The difference between them depends, as far as I can understand right now, on whether lines surrounding the changed lines are duplicated in the reference file (file “A” lets say) and where they occur in the reference file relative to the changes.

I’ve got rough ideas about how this could work (which may be very wrong!) but I suspect working out the logic (if you haven’t done it before) is a good part of the challenge as you then have to map that to rust.

We can talk about the logic in the comments here though, of course.

Also, an alteration here is really a deletion + addition, so not necessary, but it makes sense to me so I might try to implement it.

Otherwise, the output is open ended I’d say … whatever makes sense to you. My first thought is JSON (which presents a chance to maybe try serde?). Something like:

[
  {
    "start_line_no": 0,
    "end_line_no": 1,
    "lines": ["this is a line of text", "and another"],
    "type": "unchanged"
  },

  {
    "start_line_no": 2,
    "end_line_no": 3,
    "lines": ["an added line",],
    "type": "addition"
  },

  ...
]

Probably a few complications there I haven’t thought about, but you get the idea. You could even go further and supplement the program with a renderer that takes this JSON and produces HTML or something else.

Time

I’d say we can have a decent shot at this in a week. So how about I post again in a week asking how we all did.