So, here’s my attempt
The first portion (^.?$
) matches all lines of 0 or 1 characters.
The second portion (^(..+?)\1+$
) is more complicated:
(..+?)
is a capture group that matches the first character in any line, followed by a smallest possible non-zero number of characters such that (2) still matches (note that the minimum length of this match is 2)\1+
matches as many as possible (and more than 0) repeats of the (1) group
I think what this does is match any line consisting of a single character with the length
- divisible by some number (due to the more than 0 condition in (2), so that there have to be repeats in the string), that’s not
1
(due to the note in (1), so that the repeating portion has to be at least 2 characters long), or- the length itself (due to the more than 0 condition in the (2), so that there is at least one repetition)
Therefore, combined with the first portion, it matches all lines of the same character whose lengths are composite (non-prime) numbers? (it will also match any line of length 1, and all lines consisting of the same string repeated more than one time)
TBH Rust is pretty nice, it borrows (pun intended) a lot of ideas from the functional world (algebraic data types, traits, closures, affine types to an extent, composition over inheritance, and the general vibe of type-driven development), but it’s much easier to write fast, efficient code, integrate with decades of libraries in imperative languages, and the ecosystem somehow feels mature already.