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

前端 未结 4 1721
一个人的身影
一个人的身影 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:13

    You may use .into():

    let a = true;
    let b: i32 = a.into();
    println!("{}", b); // 1
    
    let z: isize = false.into();
    println!("{}", z); // 0
    

    playground

提交回复
热议问题