Code Example Template
Deep-Dive System Documentation
Booleans in Rust
The bool type represents logical boolean values in Rust and has two possible values: true and false. Booleans are exactly one byte in size and are highly integrated with Rust's strict compile-time type-safety rules—they do not implicitly convert to integers like in C/C++.
let is_active: bool = true;
if is_active {
println!("System active.");
}
Useful Methods
.then<T, F>(f: F) -> Option<T> where F: FnOnce() -> T
Returns Some(f()) if the bool is true, and None otherwise. Use this to conditionally execute lazy initialization.
let debug_enabled = true;
let log_buffer = debug_enabled.then(|| vec![0u8; 1024]);
assert_eq!(log_buffer.is_some(), true);
.then_some<T>(t: T) -> Option<T>
Returns Some(t) if the bool is true, and None otherwise. Note that the argument t is evaluated eagerly.
let has_permission = false;
let access_token = has_permission.then_some("SECRET_TOKEN");
assert_eq!(access_token, None);
Quick Reference Guide
let t: bool = true;
let f: bool = false;