Booleans
bool is Loom’s Boolean type with two values: true and false.
It is a shorthand for bit (0/1) with logical semantics and is used in control flow, predicates, and boolean algebra.
- Values:
true,false(alias of1and0at the storage level) - Storage: 1 bit (stand-alone variables typically occupy 1 byte; arrays are packed)
- Interchange: Zero-cost aliasing with
bit; explicit casts for integers
Literals & basics
let a: bool = true
let b: bool = false
var ok = (3 < 5) # truePrinting: print(true) → true
Control flow
bool guards if, while, match guards, etc.
if ok { println("it passed") }
while cond() {
step()
}Operators
Logical (short-circuit)
!p(NOT)p and q(AND) — evaluatesqonly ifpistruep or q(OR) — evaluatesqonly ifpisfalse
Bitwise (non–short-circuit, defined for bool)
p & q,p | q,p ^ q(evaluate both sides, returnbool)- Equality:
p == q,p != q - Ordering (
< <= > >=) is not defined forbool; cast tobit/int if needed.
let p = true
let q = false
let x = p and expensive() # short-circuits
let y = p & expensive() # always evaluates
let z = p ^ q # trueConversions
bool ⇄ bit
bool → bit:true → 1,false → 0bit → bool:1 → true,0 → false- Both are zero-cost (same underlying bit), but casts make intent clear.
let b: bit = true as bit
let t: bool = (1 as bit) as boolbool ⇄ integers
-
Explicit casts only (avoid accidental logic–arithmetic mixing):
bool → iN/uN:true → 1,false → 0iN/uN → bool:(value != 0)or(value & 1) != 0, via cast/helpers
let n: u32 = (flag as u32) # 0 or 1
let ok: bool = (count as i32) != 0
# Helpers if provided:
# let ok = bool.from_int(count) # true if nonzero
# let v = flag.to_int() # 0 or 1bool ⇄ string
let s = bool.to_string(true) # "true"
let v = bool.parse("false") # false (throws on invalid)
let o = bool.parse_opt("yes") # NoneCollections & packing
[]bool(slices) and arrays are bit-packed; indexing is O(1).- Iteration yields
bool:
var marks: [bool; 8]
for i in 0..marks.len() { marks[i] = (i % 2 == 0) }
for m in marks { if m { /* ... */ } }For large bitsets plus bitwise bulk ops, prefer BitVec/BitArray (see bit).
Standard predicates & helpers
is_ascii_alnum(ch) -> bool # example: library predicates return bool
all(xs: []bool) -> bool # true if all true
any(xs: []bool) -> bool # true if any truePatterns & guidance
- Use
boolfor logic and control flow; usebitfor numeric bit math and compact masks. - Prefer
and/orin conditionals for short-circuiting; use&/|/^when you require both sides to evaluate (e.g., flag combinators). - When mixing with integers, cast explicitly to document intent.
Examples
Guarded initialization
var initialized: bool = false
var handle: i32 = -1
if !initialized {
handle = open()
initialized = (handle >= 0)
}Combining feature flags
let has_gpu: bool = probe_gpu()
let want_gpu: bool = cfg.use_gpu
let use_gpu = has_gpu and want_gpuFiltering with predicates
pub func count_true(xs: []bool): usize {
var n: usize = 0
for v in xs { if v { n += 1 } }
ret n
}FAQs
Is bool just a typedef of bit?
Yes—same underlying representation, but bool carries logical intent, has true/false literals, and is accepted directly by control flow.
Can I rely on sizeof(bool) == 1?
Standalone values typically occupy 1 byte; arrays/slices are bit-packed. For FFI, match the foreign ABI explicitly (e.g., map to C _Bool or uint8_t).
Why no </> on bool?
To avoid accidental ordinal comparisons; cast to bit/int if you need 0/1 ordering.
See also
bit(numeric single-bit type,BitVec,BitArray)- Integers (
i*,u*) for arithmetic - Control flow (
if,while,matchguards)