Boolean
A boolean holds one of exactly two values: true or false. It is the answer to a yes-or-no question — is the file open, has the player won — and the rest of the course leans on it constantly: every comparison produces one, and if and while choose what to run by one.
true and false
true and false are the only boolean literals, and either one makes a bool:
let ready = true;
let finished = false;
let visible: bool = true;
The boolean family
bool is the everyday spelling. It is another name for bool8, a boolean stored in one byte. The wider types hold the same two values in more space:
| Type | Size | Use |
|---|---|---|
bool = bool8 | 1 byte | Everyday code |
bool16 | 2 bytes | Talking to code in other languages |
bool32 | 4 bytes | The Windows API's BOOL, for one |
bool64 | 8 bytes | Matching other 8-byte layouts |
bool128 … bool512 | 16 to 64 bytes | Matching wider layouts |
let flag32: bool32 = true;
They exist for interoperating with C and operating-system APIs, where a boolean's size is part of the agreement. In your own code, use bool.
Where booleans come from
You will rarely write true or false by hand. Most booleans are produced by questions the program asks:
flowchart LR
cmp["`score > 10`<br/>(a comparison)"] --> b(("`bool`"))
logic["`ready && !finished`<br/>(logic)"] --> b
b --> ifs["`if … { }`"]
b --> wh["`while … { }`"]
b --> tern["`cond ? a : b`"]Comparison and Logical show how to produce them; If and While show how to use them.
The program
The whole lesson is one package in the Examples repository. Its comments explain every step.
// A boolean holds one of exactly two values, `true` or `false`. It is the answer to a yes-or-no
// question — is the file open, has the player won — and later parts lean on it constantly: a
// comparison produces one, and `if` and `while` choose what to run by one.
//
// `bool` is the everyday spelling. It is another name for `bool8`, a boolean stored in one byte.
// `bool16`, `bool32` and `bool64` hold the same two values in two, four and eight bytes, and
// `bool128`, `bool256` and `bool512` in 16, 32 and 64. They exist to match the layout of data
// shared with code written in other languages: the Windows API, for one, uses a four-byte
// boolean, which is a `bool32` here.
import Io::PrintLine;
func Main() -> int {
// `true` and `false` are the only boolean literals, and either one makes a `bool`.
let ready = true;
let finished = false;
PrintLine("ready {}", ready);
PrintLine("finished {}", finished);
// The type can be written out, as with any variable.
let visible: bool = true;
PrintLine("visible {}", visible);
// The wider types print the same way. Only their size differs.
let flag8: bool8 = true;
let flag16: bool16 = false;
let flag32: bool32 = true;
let flag64: bool64 = false;
PrintLine("bool8 {}", flag8);
PrintLine("bool16 {}", flag16);
PrintLine("bool32 {}", flag32);
PrintLine("bool64 {}", flag64);
let flag128: bool128 = true;
let flag256: bool256 = false;
let flag512: bool512 = true;
PrintLine("bool128 {}", flag128);
PrintLine("bool256 {}", flag256);
PrintLine("bool512 {}", flag512);
// A boolean is not a number. Some languages treat 0 as false and 1 as true; Rux does not mix
// them up on its own:
//
// let on: bool = 1;
// error: cannot assign 'int' to 'bool8'
//
// The message names `bool8` because that is what `bool` is. Turning a number into a boolean,
// or back, is a conversion you ask for, and the Convert lesson shows how.
return 0;
}
Run it
cd Examples/Basics/Boolean
rux run
ready true
finished false
visible true
bool8 true
bool16 false
bool32 true
bool64 false
bool128 true
bool256 false
bool512 true
Common mistakes
Try it yourself
- Declare
let raining = true;andlet umbrella = false;and print a sentence using both. - Try
let on: bool = 1;and read the message. - Look ahead: in the Comparison lesson, find the line that produces a
boolwithout writingtrueorfalse.
Learn more
1.6 Float
Store fractional numbers in float64, float32 and the float alias, meet the narrow and wide widths from float8 to float512, and see why a float is only ever an approximation.
1.8 Character
Write single characters with 'A', escapes and \u{...}, and meet char and the sized character types char8 to char512.