equivalent code in rust and c++