bool 是否保证为 1 个字节? [英] Is bool guaranteed to be 1 byte?

查看:58
本文介绍了bool 是否保证为 1 个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Rust 文档bool 上含糊不清代码>的大小.
它是保证是 1 个字节,还是像 C++ 一样未指定?

The Rust documentation is vague on bool's size.
Is it guaranteed to be 1 byte, or is it unspecified like in C++?

fn main() {
    use std::mem;
    println!("{}",mem::size_of::<bool>()); //always 1?
}

推荐答案

Rust 将 i1 发送到 LLVM for bool 并依赖于它产生的任何内容.对于 Rust 目前支持的所有平台,LLVM 使用 i8(一个字节)来表示内存中的 i1.另一方面,未来也没有确定性,因为到目前为止,Rust 开发人员一直拒绝承诺使用特定的 bool 表示.

Rust emits i1 to LLVM for bool and relies on whatever it produces. LLVM uses i8 (one byte) to represent i1 in memory for all the platforms supported by Rust for now. On the other hand, there's no certainty about the future, since the Rust developers have been refusing to commit to the particular bool representation so far.

因此,它由当前的实现保证,但不受任何规范的保证.

So, it's guaranteed by the current implementation but not guaranteed by any specifications.

您可以在此 RFC 讨论和链接的 PR 中找到更多详细信息和问题.

You can find more details in this RFC discussion and the linked PR and issue.

请参阅下面的答案,以获取有关自此答案发布以来 Rust 中引入的更改的更多信息.

Please, see the answer below for more information about changes introduced in Rust since this answer had been published.

这篇关于bool 是否保证为 1 个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆