Is there a modulus (not remainder) function / operation?

前端 未结 4 2010
粉色の甜心
粉色の甜心 2020-12-14 05:31

In Rust (like most programming languages), the % operator performs the remainder operation, not the modulus operation. These operations have d

4条回答
  •  一生所求
    2020-12-14 06:11

    RFC 2196 adds a couple of integer methods related to euclidian division. Specifically, the rem_euclid method (example link for i32) is what you are searching for:

    println!("{}", -1i32 % 4);                // -1
    println!("{}", (-21i32).rem_euclid(4));   // 3
    

    This method is available in rustc 1.38.0 (released on 2019-09-27) and above.

提交回复
热议问题