0x001
布尔类型有两种:true和false。
如果将bool类型转换为整数,true将为1,false将为0。
0x002
类型声明
fn main(){
let x = false;
let y: bool = true;
println!("x = {}, Y = {}", x,y);
}
布尔运算
bool实现了各种特征,例如BitAnd、BitOr、Not等,它们允许我们使用&、|和!执行布尔运算。
fn main(){
let a = false | true;
let b: bool = true & false;
let c = false || true;
let d: bool = true && false;
println!("a = {}, b = {}, c = {}, d = {}", a, b, c, d);
}
示例片段1
let praise_the_borrow_checker = true;
if praise_the_borrow_checker {
println!("oh,yeah!");
}else {
println!("what?!");
//
match praise_the_borrow_checker {
true => println!("keep praiseing!"),
false => println!("you should praise!"),
}
示例片段2
fn main(){
let x = true;
let y: bool = !x;
println!("{} {}",x,y);
let z = x && y;
println!("{}",z);
let z = x || y;
println!("{}",z);
let z = x & y;
println!("{}",z);
let z = x | y;
println!("{}",z);
let z = x ^ y;
println!("{}",z);
test(1,2);
}
fn test(x:i32, y:i32){
let z: bool = x < y;
println!("{}", z);
if x > y {
println!("{}", "x >y");
}else {
println!("{}", "x<y");
}
}
程序结果:
true false
false
true
false
true
true
true
x < y