For the purpose of representing an object as a number (for use in a hash map or for comparison), most languages' built-in types come with a solid hash function for exactly that purpose, so building your own is almost never a good idea, unless a lot of time and effort goes into it. The better choice is to use what's built-in, or use a library that provides tested and proven methods.
It is important though to know how those functions are built, so let's create a trivial implementation to analyze the basic principles. The following example is one that uses the XOR operation on the previous and current byte to save their binary differences, then shifts it to the left up to four times (to fill up the u32 type):
pub fn hashcode(bytes: &[u8]) -> u32 {
let mut a = 0_u32;
for (i, b) in bytes.iter().enumerate() {
a ^= *b as u32;
a <<= i % 4; }
a
}
When this function is applied to a range of repeated letter strings, how are the...