Quantcast
Channel: How do I convert a boolean to an integer in Rust? - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Answer by Tim Diekmann for How do I convert a boolean to an integer in Rust?

$
0
0

A boolean value in Rust is guaranteed to be 1 or 0:

The bool represents a value, which could only be either true or false. If you cast a bool into an integer, true will be 1 and false will be 0.

A boolean value, which is neither0nor1 is undefined behavior:

A value other than false (0) or true (1) in a bool.

Therefore, you can just cast it to a primitive:

assert_eq!(0, false as i32);assert_eq!(1, true as i32);

Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>