Primitive Type bool1.0.0[]

Expand description

布尔类型。

bool 代表一个值,它只能是 truefalse。 如果将 bool 转换为整数,则 true 表示为 1,false 表示为 0。

基本用法

bool 实现了各种 traits,例如 BitAndBitOrNot 等,允许我们使用 &|! 执行布尔运算。

if 需要一个 bool 值作为它的条件。 assert! 是测试中的一个重要宏,检查表达式是否为 true,如果不是则为 panics。

let bool_val = true & false | false;
assert!(!bool_val);
Run

Examples

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);
Run

Implementations

🔬 This is a nightly-only experimental API. (bool_to_option #80967)

如果 booltrue,则返回 Some(t),否则返回 None

Examples
#![feature(bool_to_option)]

assert_eq!(false.then_some(0), None);
assert_eq!(true.then_some(0), Some(0));
Run

如果 booltrue,则返回 Some(f()),否则返回 None

Examples
assert_eq!(false.then(|| 0), None);
assert_eq!(true.then(|| 0), Some(0));
Run

Trait Implementations

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

应用 & 运算符后的结果类型。

执行 & 操作。 Read more

执行 &= 操作。 Read more

执行 &= 操作。 Read more

执行 &= 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

应用 | 运算符后的结果类型。

执行 | 操作。 Read more

执行 |= 操作。 Read more

执行 |= 操作。 Read more

执行 |= 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

应用 ^ 运算符后的结果类型。

执行 ^ 操作。 Read more

执行 ^= 操作。 Read more

执行 ^= 操作。 Read more

执行 ^= 操作。 Read more

返回值的副本。 Read more

source 执行复制分配。 Read more

使用给定的格式化程序格式化该值。 Read more

Returns the default value of false

使用给定的格式化程序格式化该值。 Read more

Converts a bool to a u64. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(u64::from(true), 1);
assert_eq!(u64::from(false), 0);
Run

Converts a bool to a u16. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(u16::from(true), 1);
assert_eq!(u16::from(false), 0);
Run

Converts a bool to a i8. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(i8::from(true), 1);
assert_eq!(i8::from(false), 0);
Run

Converts a bool to a usize. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(usize::from(true), 1);
assert_eq!(usize::from(false), 0);
Run

Converts a bool to a i64. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(i64::from(true), 1);
assert_eq!(i64::from(false), 0);
Run

Converts a bool to a u32. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(u32::from(true), 1);
assert_eq!(u32::from(false), 0);
Run

Converts a bool to a i16. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(i16::from(true), 1);
assert_eq!(i16::from(false), 0);
Run

Converts a bool to a isize. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(isize::from(true), 1);
assert_eq!(isize::from(false), 0);
Run

bool 转换为 AtomicBool

Examples
use std::sync::atomic::AtomicBool;
let atomic_bool = AtomicBool::from(true);
assert_eq!(format!("{:?}", atomic_bool), "true")
Run

Converts a bool to a i32. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(i32::from(true), 1);
assert_eq!(i32::from(false), 0);
Run

Converts a bool to a u8. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(u8::from(true), 1);
assert_eq!(u8::from(false), 0);
Run

Converts a bool to a u128. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(u128::from(true), 1);
assert_eq!(u128::from(false), 0);
Run

Converts a bool to a i128. The resulting value is 0 for false and 1 for true values.

Examples
assert_eq!(i128::from(true), 1);
assert_eq!(i128::from(false), 0);
Run

从字符串中解析 bool

产生 Result<bool, ParseBoolError>,因为 s 实际上可以解析,也可以不解析。

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

可以从解析中返回的相关错误。

将该值输入给定的 HasherRead more

将这种类型的切片送入给定的 Hasher 中。 Read more

应用 ! 运算符后的结果类型。

执行一元 ! 操作。 Read more

应用 ! 运算符后的结果类型。

执行一元 ! 操作。 Read more

此方法返回 selfother 之间的 OrderingRead more

比较并返回两个值中的最大值。 Read more

比较并返回两个值中的最小值。 Read more

将值限制在某个时间间隔内。 Read more

此方法测试 selfother 值是否相等,并由 == 使用。 Read more

此方法测试 !=

如果存在,则此方法返回 selfother 值之间的顺序。 Read more

此方法测试的内容少于 (对于 selfother),并且由 < 操作员使用。 Read more

此方法测试小于或等于 (对于 selfother),并且由 <= 运算符使用。 Read more

此方法测试大于 (对于 selfother),并且由 > 操作员使用。 Read more

此方法测试是否大于或等于 (对于 selfother),并且由 >= 运算符使用。 Read more

Auto Trait Implementations

Blanket Implementations

获取 selfTypeIdRead more

从拥有的值中一成不变地借用。 Read more

从拥有的值中借用。 Read more

执行转换。

执行转换。

获得所有权后的结果类型。

从借用的数据创建拥有的数据,通常是通过克隆。 Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into #41263)

使用借来的数据来替换拥有的数据,通常是通过克隆。 Read more

将给定值转换为 StringRead more

发生转换错误时返回的类型。

执行转换。

发生转换错误时返回的类型。

执行转换。