Bool and Unit Primitives
Bool และ Unit types
bool Type
Section titled “bool Type”fn main() { // Boolean values let is_active = true; let is_complete = false;
println!("active: {}, complete: {}", is_active, is_complete);
// From comparison let x = 10; let is_positive = x > 0; let is_even = x % 2 == 0;
println!("positive: {}, even: {}", is_positive, is_even);
// Boolean operations let a = true; let b = false;
println!("\n=== Logical Operations ==="); println!("a && b = {}", a && b); // AND println!("a || b = {}", a || b); // OR println!("!a = {}", !a); // NOT println!("a ^ b = {}", a ^ b); // XOR
// Short-circuit evaluation fn check() -> bool { println!("check() called"); true }
println!("\n=== Short-circuit ==="); let _ = false && check(); // check() not called let _ = true || check(); // check() not called
// then / then_some println!("\n=== then methods ==="); let result = is_active.then(|| "Active!"); println!("then: {:?}", result);
let result = is_complete.then_some("Complete"); println!("then_some: {:?}", result);}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google