How do I convert a boolean to an integer in Rust?

前端 未结 4 1758
一个人的身影
一个人的身影 2021-01-17 10:03

How do I convert a boolean to an integer in Rust? As in, true becomes 1, and false becomes 0.

4条回答
  •  死守一世寂寞
    2021-01-17 10:12

    Use an if statement:

    if some_boolean { 1 } else { 0 }
    

    See also:

    • How can I port C++ code that uses the ternary operator to Rust?

提交回复
热议问题