Primitive Type bool
1.0.0 ·Expand description
布尔类型。
bool 代表一个值,它只能是 true 或 false。
如果将 bool 转换为整数,则 true 将为 1,false 将为 0.
基本用法
bool 实现了各种 traits,例如 BitAnd、BitOr、Not 等,允许我们使用 &、| 和 ! 执行布尔运算。
if 需要一个 bool 值作为它的条件。
assert! 是测试中的一个重要宏,检查表达式是否为 true,如果不是则为 panics。
let bool_val = true & false | false;
assert!(!bool_val);RunExamples
bool 用法的一个简单示例:
let praise_the_borrow_checker = true;
// 使用 `if` 有条件
if praise_the_borrow_checker {
println!("oh, yeah!");
} else {
println!("what?!!");
}
// ... 或者,匹配模式
match praise_the_borrow_checker {
true => println!("keep praising!"),
false => println!("you should praise!"),
}Run另外,由于 bool 实现了 Copy trait,因此我们不必担心移动语义 (就像整数和浮点图元一样)。
现在将 bool 强制转换为整数类型的示例:
assert_eq!(true as i32, 1);
assert_eq!(false as i32, 0);RunImplementations§
source§impl bool
impl bool
1.62.0 · sourcepub fn then_some<T>(self, t: T) -> Option<T>
pub fn then_some<T>(self, t: T) -> Option<T>
如果 bool 是 true,则返回 Some(t),否则返回 None。
传递给 then_some 的参数被热切地评估; 如果要传递函数调用的结果,建议使用 then,它是惰性求值的。
Examples
assert_eq!(false.then_some(0), None);
assert_eq!(true.then_some(0), Some(0));Runlet mut a = 0;
let mut function_with_side_effects = || { a += 1; };
true.then_some(function_with_side_effects());
false.then_some(function_with_side_effects());
// `a` 增加了两次,因为传递给 `then_some` 的值被急切地评估。
assert_eq!(a, 2);RunTrait Implementations§
source§impl<T, const LANES: usize> BitAnd<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAnd<Mask<T, LANES>> for boolwhere T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
source§impl<T, const LANES: usize> BitAnd<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAnd<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
1.22.0 · source§impl BitAndAssign<&bool> for bool
impl BitAndAssign<&bool> for bool
source§fn bitand_assign(&mut self, other: &bool)
fn bitand_assign(&mut self, other: &bool)
执行
&= 操作。 Read moresource§impl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitAndAssign<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
source§fn bitand_assign(&mut self, rhs: bool)
fn bitand_assign(&mut self, rhs: bool)
执行
&= 操作。 Read more1.8.0 · source§impl BitAndAssign<bool> for bool
impl BitAndAssign<bool> for bool
source§fn bitand_assign(&mut self, other: bool)
fn bitand_assign(&mut self, other: bool)
执行
&= 操作。 Read moresource§impl<T, const LANES: usize> BitOr<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitOr<Mask<T, LANES>> for boolwhere T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
source§impl<T, const LANES: usize> BitOr<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitOr<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
1.22.0 · source§impl BitOrAssign<&bool> for bool
impl BitOrAssign<&bool> for bool
source§fn bitor_assign(&mut self, other: &bool)
fn bitor_assign(&mut self, other: &bool)
执行
|= 操作。 Read moresource§impl<T, const LANES: usize> BitOrAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitOrAssign<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
source§fn bitor_assign(&mut self, rhs: bool)
fn bitor_assign(&mut self, rhs: bool)
执行
|= 操作。 Read more1.8.0 · source§impl BitOrAssign<bool> for bool
impl BitOrAssign<bool> for bool
source§fn bitor_assign(&mut self, other: bool)
fn bitor_assign(&mut self, other: bool)
执行
|= 操作。 Read moresource§impl<T, const LANES: usize> BitXor<Mask<T, LANES>> for boolwhere
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitXor<Mask<T, LANES>> for boolwhere T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
source§impl<T, const LANES: usize> BitXor<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitXor<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
1.22.0 · source§impl BitXorAssign<&bool> for bool
impl BitXorAssign<&bool> for bool
source§fn bitxor_assign(&mut self, other: &bool)
fn bitxor_assign(&mut self, other: &bool)
执行
^= 操作。 Read moresource§impl<T, const LANES: usize> BitXorAssign<bool> for Mask<T, LANES>where
T: MaskElement,
LaneCount<LANES>: SupportedLaneCount,
impl<T, const LANES: usize> BitXorAssign<bool> for Mask<T, LANES>where T: MaskElement, LaneCount<LANES>: SupportedLaneCount,
source§fn bitxor_assign(&mut self, rhs: bool)
fn bitxor_assign(&mut self, rhs: bool)
执行
^= 操作。 Read more1.8.0 · source§impl BitXorAssign<bool> for bool
impl BitXorAssign<bool> for bool
source§fn bitxor_assign(&mut self, other: bool)
fn bitxor_assign(&mut self, other: bool)
执行
^= 操作。 Read more1.24.0 · source§impl From<bool> for AtomicBool
impl From<bool> for AtomicBool
source§impl FromStr for bool
impl FromStr for bool
source§fn from_str(s: &str) -> Result<bool, ParseBoolError>
fn from_str(s: &str) -> Result<bool, ParseBoolError>
从字符串中解析 bool。
唯一可接受的值是 "true" 和 "false"。
任何其他输入都将返回错误。
Examples
use std::str::FromStr;
assert_eq!(FromStr::from_str("true"), Ok(true));
assert_eq!(FromStr::from_str("false"), Ok(false));
assert!(<bool as FromStr>::from_str("not even a boolean").is_err());Run注意,在许多情况下,str 上的 .parse() 方法更合适。
assert_eq!("true".parse(), Ok(true));
assert_eq!("false".parse(), Ok(false));
assert!("not even a boolean".parse::<bool>().is_err());Run§type Err = ParseBoolError
type Err = ParseBoolError
可以从解析中返回的相关错误。