将 `bool` 转换为 `u8` [英] Transmuting `bool` to `u8`
本文介绍了将 `bool` 转换为 `u8`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下代码与 amd64 上的 Rust 1.8 一起正常工作.
The following code works correctly with Rust 1.8 on amd64.
use std::mem;
fn main() {
let f: u8 = unsafe { mem::transmute(false) };
let t: u8 = unsafe { mem::transmute(true) };
assert_eq!(0, f);
assert_eq!(1, t);
}
我的问题是,我可以假设这总是有效吗?我试图找到关于 bool
表示的参考,但我只找到了 this 和 this,但我认为这不是权威.
My question is, can I assume that this will always works? I tried to find a reference about bool
representation, but I only found this and this, but I think that is not authoritative.
推荐答案
从来没有任何 RFC 被采用来定义 bool
的表示.也就是说,作为一个实际问题,它不太可能改变.
There was never any RFC adopted defining the representation of bool
. That said, as a practical matter it's very unlikely it will change.
这篇关于将 `bool` 转换为 `u8`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文