Implement equivalent algorithm in Rust if need more performance

C++ would also fit but let's prefer Rust as it's not a headache to use it in this context, as there isn't multithreading.