The Rust Programming Language — Primitives — Booleans, Conditionals, Statements and Expressions
Published in
4 min readOct 9, 2023
In this article, we are going to learn about subtle nuances of The Rust Programming Language when using Booleans, Conditionals, Statements and Expressions. We’ll also get to learn that Rust omits some of the features from other languages intentionally.
Booleans:
Let us start with Booleans. As all the other languages, we only have true and false. Look at the below code snippet:
fn main() {
let should_go_fast = true;
let should_go_slow = false;
println!("should_go_fast {}", should_go_fast);
println!("should_go_slow {}", should_go_slow);
}
You can also cast the booleans to u8
and the output would be 1
for true
and 0
for false
. Look at the below code snippet:
fn main() {
let should_go_fast = true;
let should_go_slow = false;
println!("should_go_fast as u8 {}", should_go_fast as u8);
println!("should_go_slow as u8 {}", should_go_slow as u8);
}