Is bool guaranteed to be 1 byte?
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?
}
size rust primitive-types
add a comment |
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?
}
size rust primitive-types
1
This is related to stackoverflow.com/q/36924530/5189607
– malbarbo
Jul 1 '16 at 22:04
See also What is the correct type for returning a C99bool
to Rust via the FFI?.
– Shepmaster
Nov 13 '18 at 21:32
add a comment |
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?
}
size rust primitive-types
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?
}
size rust primitive-types
size rust primitive-types
edited Jun 27 '16 at 13:53
Shepmaster
150k13291430
150k13291430
asked Jun 27 '16 at 6:26
Trevor HickeyTrevor Hickey
16.7k1393187
16.7k1393187
1
This is related to stackoverflow.com/q/36924530/5189607
– malbarbo
Jul 1 '16 at 22:04
See also What is the correct type for returning a C99bool
to Rust via the FFI?.
– Shepmaster
Nov 13 '18 at 21:32
add a comment |
1
This is related to stackoverflow.com/q/36924530/5189607
– malbarbo
Jul 1 '16 at 22:04
See also What is the correct type for returning a C99bool
to Rust via the FFI?.
– Shepmaster
Nov 13 '18 at 21:32
1
1
This is related to stackoverflow.com/q/36924530/5189607
– malbarbo
Jul 1 '16 at 22:04
This is related to stackoverflow.com/q/36924530/5189607
– malbarbo
Jul 1 '16 at 22:04
See also What is the correct type for returning a C99
bool
to Rust via the FFI?.– Shepmaster
Nov 13 '18 at 21:32
See also What is the correct type for returning a C99
bool
to Rust via the FFI?.– Shepmaster
Nov 13 '18 at 21:32
add a comment |
2 Answers
2
active
oldest
votes
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.
You can find more details in this RFC discussion and the linked PR and issue.
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
add a comment |
While historically there was a wish to avoid committing to a more specific representation, it was eventually decided in January 2018 that bool
should provide the following guarantees:
- The definition of
bool
is equivalent to the C99 definition ofBool_
- For all currently supported platforms, the size of
bool
is exactly 1.
The documentation has been updated accordingly. In the Rust reference, bool
is defined as thus:
The
bool
type is a datatype which can be eithertrue
orfalse
. The boolean type uses one byte of memory. [...]
It has also been documented since 1.25.0 that the output of std::mem::size_of::<bool>()
is 1.
As such, one can indeed rely on bool
being 1 byte (and if this is ever to change, it will be a pretty loud one).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38047167%2fis-bool-guaranteed-to-be-1-byte%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
You can find more details in this RFC discussion and the linked PR and issue.
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
add a comment |
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.
You can find more details in this RFC discussion and the linked PR and issue.
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
add a comment |
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.
You can find more details in this RFC discussion and the linked PR and issue.
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.
You can find more details in this RFC discussion and the linked PR and issue.
edited Jul 1 '16 at 21:22
answered Jun 27 '16 at 8:15
Andrew LyginAndrew Lygin
4,79812131
4,79812131
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
add a comment |
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
In particular this comment which explicitly states that they don't want to tie down the representation.
– Chris Emerson
Jun 27 '16 at 8:56
add a comment |
While historically there was a wish to avoid committing to a more specific representation, it was eventually decided in January 2018 that bool
should provide the following guarantees:
- The definition of
bool
is equivalent to the C99 definition ofBool_
- For all currently supported platforms, the size of
bool
is exactly 1.
The documentation has been updated accordingly. In the Rust reference, bool
is defined as thus:
The
bool
type is a datatype which can be eithertrue
orfalse
. The boolean type uses one byte of memory. [...]
It has also been documented since 1.25.0 that the output of std::mem::size_of::<bool>()
is 1.
As such, one can indeed rely on bool
being 1 byte (and if this is ever to change, it will be a pretty loud one).
add a comment |
While historically there was a wish to avoid committing to a more specific representation, it was eventually decided in January 2018 that bool
should provide the following guarantees:
- The definition of
bool
is equivalent to the C99 definition ofBool_
- For all currently supported platforms, the size of
bool
is exactly 1.
The documentation has been updated accordingly. In the Rust reference, bool
is defined as thus:
The
bool
type is a datatype which can be eithertrue
orfalse
. The boolean type uses one byte of memory. [...]
It has also been documented since 1.25.0 that the output of std::mem::size_of::<bool>()
is 1.
As such, one can indeed rely on bool
being 1 byte (and if this is ever to change, it will be a pretty loud one).
add a comment |
While historically there was a wish to avoid committing to a more specific representation, it was eventually decided in January 2018 that bool
should provide the following guarantees:
- The definition of
bool
is equivalent to the C99 definition ofBool_
- For all currently supported platforms, the size of
bool
is exactly 1.
The documentation has been updated accordingly. In the Rust reference, bool
is defined as thus:
The
bool
type is a datatype which can be eithertrue
orfalse
. The boolean type uses one byte of memory. [...]
It has also been documented since 1.25.0 that the output of std::mem::size_of::<bool>()
is 1.
As such, one can indeed rely on bool
being 1 byte (and if this is ever to change, it will be a pretty loud one).
While historically there was a wish to avoid committing to a more specific representation, it was eventually decided in January 2018 that bool
should provide the following guarantees:
- The definition of
bool
is equivalent to the C99 definition ofBool_
- For all currently supported platforms, the size of
bool
is exactly 1.
The documentation has been updated accordingly. In the Rust reference, bool
is defined as thus:
The
bool
type is a datatype which can be eithertrue
orfalse
. The boolean type uses one byte of memory. [...]
It has also been documented since 1.25.0 that the output of std::mem::size_of::<bool>()
is 1.
As such, one can indeed rely on bool
being 1 byte (and if this is ever to change, it will be a pretty loud one).
edited Nov 13 '18 at 21:31
Shepmaster
150k13291430
150k13291430
answered Nov 13 '18 at 17:33
E_net4 wishes happy holidaysE_net4 wishes happy holidays
12.1k63468
12.1k63468
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f38047167%2fis-bool-guaranteed-to-be-1-byte%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
This is related to stackoverflow.com/q/36924530/5189607
– malbarbo
Jul 1 '16 at 22:04
See also What is the correct type for returning a C99
bool
to Rust via the FFI?.– Shepmaster
Nov 13 '18 at 21:32