Ha! Like I said, it's written in Rust but it uses Rust as if it were C with a bit different syntax. Being new to Rust and not having read The Book properly yet I even created a structure to represent 'slices' rather than using actual Rust slices. Call it "idiotomatic" rather than "idiomatic".
Sorting the letters of the worlds like that is the traditional approach. I tried something different.
From each word in the dictionary I create what I call a 'prime hash' a 64 bit integer number that will represent the word. That integer is created by:
There is a map of all the letters of the alphabet to a prime number. So that 'a' => 2, 'b' => 3, 'c' => 5... z => 101. This is just a static array.
Starting with a prime has of 1, for each letter of a word multiply the hash by the prime number the letter maps to.
When all the letters are done the prime hash is now a number that is unique to all combinations of the letters that make up the word. This is true because of the "Fundamental Theorem of Arithmetic" which states:
Any integer greater than 1 is either a prime number , or can be written as a unique product of prime numbers (ignoring the order).
That sounds very grand but you have likely come across it in highschool maths.
Now, to find anagrams becomes easy. All we have to do is to try and store the words of the input into a HashMap using their "prime hash" as the key and the word as the value. If the HashMap already has that key we know we have found an anagram.
To avoid copying data around the entire word list is held in a byte array. Which is scanned byte by byte, any line ending found indicates the end of a word, so now the word can be remembered just by storing it's start and end index in that array. There is only a single loop in my algorithm.
Code is here: insane-british-anagram-rust/main.rs at master · ZiCog/insane-british-anagram-rust · GitHub. If you dare to look. It's pretty simple but really bad Rust style.
If you can't get the Debian insane word list there are word lists on the net of course: