Boolean
Part of the Fundamentals section of Coddy's Rust journey — lesson 8 of 75.
A boolean type has only 2 possible values: true or false.
To assign a boolean value to a variable, use the keyword let followed by the variable name:
let variable_true: bool = true;
let variable_false: bool = false;In the above example, two boolean variables named variable_true and variable_false are initialized with the values true and false, respectively.
Booleans are the building blocks for creating logic in the programs we write. We have a whole chapter about logic and conditions.
Challenge
BeginnerDeclare a variable named is_logged_in and assign it the value true.
Cheat sheet
A boolean type has only 2 possible values: true or false.
To assign a boolean value to a variable:
let variable_true: bool = true;
let variable_false: bool = false;Try it yourself
fn main() {
// Type your code below
let is_logged_in: bool = ?;
// Don't change the line below
println!("is_logged_in = {}", is_logged_in);
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsString Comparison5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 33Variables Part 2
Type DeclarationNaming ConventionsType InferenceRecap - Initialize VariablesType Casting9Loops
For Over SeriesWhile LoopBreakContinueNested LoopLoop LabelsInfinite LoopRecap - Dynamic Input