-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Referencify bool type #940
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,118 @@ | ||
# Boolean type | ||
|
||
The `bool` type is a datatype which can be either `true` or `false`. The boolean | ||
type uses one byte of memory. It is used in comparisons and bitwise operations | ||
like `&`, `|`, and `!`. | ||
|
||
```rust | ||
fn main() { | ||
let x = true; | ||
let y: bool = false; // with the boolean type annotation | ||
|
||
// Use of booleans in conditional expressions | ||
if x { | ||
println!("x is true"); | ||
} | ||
} | ||
let b: bool = true; | ||
``` | ||
|
||
The *boolean type* or *bool* is a primitive data type that can take on one of | ||
two values, called *true* and *false*. | ||
|
||
Values of this type may be created using a [literal expression] using the | ||
keywords `true` and `false` corresponding to the value of the same name. | ||
|
||
This type is a part of the [language prelude] with the [name] `bool`. | ||
|
||
An object with the boolean type has a [size and alignment] of 1 each. The | ||
value false has the bit pattern `0x00` and the value true has the bit pattern | ||
`0x01`. It is [undefined behavior] for an object with the boolean type to have | ||
any other bit pattern. | ||
|
||
The boolean type is the type of many operands in various [expressions]: | ||
|
||
* The condition operand in [if expressions] and [while expressions] | ||
* The operands in [lazy boolean operator expressions][lazy] | ||
|
||
> **Note**: The boolean type acts similarly to but is not an [enumerated type]. | ||
In practice, this mostly means that constructors are not associated to the type | ||
(e.g. `bool::true`). | ||
|
||
Like all primitives, the boolean type [implements][p-impl] the | ||
[traits][p-traits] [`Clone`][p-clone], [`Copy`][p-copy], [`Sized`][p-sized], | ||
[`Send`][p-send], and [`Sync`][p-sync]. | ||
|
||
> **Note**: See the [standard library docs][std] for library operations. | ||
|
||
## Operations on boolean values | ||
|
||
<!-- This is washy wording --> When using certain operator expressions with a | ||
boolean type for its operands, they evaluate using the rules of [boolean logic]. | ||
|
||
### Logical not | ||
|
||
| `b` | [`!b`][op-not] | | ||
|- | - | | ||
| `true` | `false` | | ||
| `false` | `true` | | ||
|
||
### Logical or | ||
|
||
| `a` | `b` | [<code>a | b</code>][op-or] | | ||
|- | - | - | | ||
| `true` | `true` | `true` | | ||
| `true` | `false` | `true` | | ||
| `false` | `true` | `true` | | ||
| `false` | `false` | `false` | | ||
|
||
### Logical and | ||
|
||
| `a` | `b` | [`a & b`][op-and] | | ||
|- | - | - | | ||
| `true` | `true` | `true` | | ||
| `true` | `false` | `false` | | ||
| `false` | `true` | `false` | | ||
| `false` | `false` | `false` | | ||
|
||
### Logical xor | ||
|
||
| `a` | `b` | [`a ^ b`][op-xor] | | ||
|- | - | - | | ||
| `true` | `true` | `false` | | ||
| `true` | `false` | `true` | | ||
| `false` | `true` | `true` | | ||
| `false` | `false` | `false` | | ||
|
||
### Comparisons | ||
|
||
| `a` | `b` | [`a == b`][op-compare] | | ||
|- | - | - | | ||
| `true` | `true` | `true` | | ||
| `true` | `false` | `false` | | ||
| `false` | `true` | `false` | | ||
| `false` | `false` | `true` | | ||
|
||
| `a` | `b` | [`a > b`][op-compare] | | ||
|- | - | - | | ||
| `true` | `true` | `false` | | ||
| `true` | `false` | `true` | | ||
| `false` | `true` | `false` | | ||
| `false` | `false` | `false` | | ||
|
||
* `a != b` is the same as `!(a == b)` | ||
* `a >= b` is the same as `a == b | a > b` | ||
* `a < b` is the same as `!(a >= b)` | ||
* `a <= b` is the same as `a == b | a < b` | ||
|
||
[boolean logic]: https://en.wikipedia.org/wiki/Boolean_algebra | ||
[enumerated type]: enum.md | ||
[expressions]: ../expressions.md | ||
[if expressions]: ../expressions/if-expr.md#if-expressions | ||
[language prelude]: ../names/preludes.md#language-prelude | ||
[lazy]: ../expressions/operator-expr.md#lazy-boolean-operators | ||
[literal expression]: ../expressions/literal-expr.md | ||
[name]: ../names.md | ||
[op-and]: ../expressions/operator-expr.md#arithmetic-and-logical-binary-operators | ||
[op-compare]: ../expressions/operator-expr.md#comparison-operators | ||
[op-not]: ../expressions/operator-expr.md#negation-operators | ||
[op-or]: ../expressions/operator-expr.md#arithmetic-and-logical-binary-operators | ||
[op-xor]: ../expressions/operator-expr.md#arithmetic-and-logical-binary-operators | ||
[p-clone]: ../special-types-and-traits.md#clone | ||
[p-copy]: ../special-types-and-traits.md#copy | ||
[p-impl]: ../items/implementations.md | ||
[p-send]: ../special-types-and-traits.md#send | ||
[p-sized]: ../special-types-and-traits.md#sized | ||
[p-sync]: ../special-types-and-traits.md#sync | ||
[p-traits]: ../items/traits.md | ||
[size and alignment]: ../type-layout.md#size-and-alignment | ||
[std]: ../../std/primitive.bool.html | ||
[undefined behavior]: ../behavior-considered-undefined.md | ||
[while expressions]: ../expressions/loop-expr.md#predicate-loops |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this doesn't list other traits like the operator traits? It does seem like a little bit of a gray area of which trait impls are part of the "language", but it is not clear to me how that line is drawn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just referenced the traits on the special traits page, and didn't think too much about the operator traits. It is an oversight, sorta. There's inherent behavior with the operators themselves, but the trait implementations are library-side:
Comparison operators are also built-in that way.
How about I add truth tables for all the things you can do with a boolean, and then link to those from the operator expressions?