Skip to content

Commit f5d4ea8

Browse files
committed
auto merge of #6748 : steveklabnik/rust/bool_docs, r=thestinger
There was some before, but now we have a big header, as well as lots of individual bits of documentation.
2 parents 1f8c4b0 + 9bb8077 commit f5d4ea8

File tree

1 file changed

+194
-19
lines changed

1 file changed

+194
-19
lines changed

src/libstd/bool.rs

+194-19
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,190 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Boolean logic
11+
/*!
12+
13+
The `bool` module contains useful code to help work with boolean values.
14+
15+
A quick summary:
16+
17+
## Trait implementations for `bool`
18+
19+
Implementations of the following traits:
20+
21+
* `FromStr`
22+
* `Ord`
23+
* `TotalOrd`
24+
* `Eq`
25+
26+
## Various functions to compare `bool`s
27+
28+
All of the standard comparison functions one would expect: `and`, `eq`, `or`,
29+
and more.
30+
31+
Also, a few conversion functions: `to_bit` and `to_str`.
32+
33+
Finally, some inquries into the nature of truth: `is_true` and `is_false`.
34+
35+
*/
1236

1337
#[cfg(not(test))]
1438
use cmp::{Eq, Ord, TotalOrd, Ordering};
1539
use option::{None, Option, Some};
1640
use from_str::FromStr;
1741

18-
/// Negation / inverse
42+
/**
43+
* Negation of a boolean value.
44+
*
45+
* # Examples
46+
* ~~~
47+
* rusti> core::bool::not(true)
48+
* false
49+
* ~~~
50+
* rusti> core::bool::not(false)
51+
* true
52+
* ~~~
53+
*/
1954
pub fn not(v: bool) -> bool { !v }
2055

21-
/// Conjunction
56+
/**
57+
* Conjunction of two boolean values.
58+
*
59+
* # Examples
60+
* ~~~
61+
* rusti> core::bool::and(true, false)
62+
* false
63+
* ~~~
64+
* rusti> core::bool::and(true, true)
65+
* true
66+
* ~~~
67+
*/
2268
pub fn and(a: bool, b: bool) -> bool { a && b }
2369

24-
/// Disjunction
70+
/**
71+
* Disjunction of two boolean values.
72+
*
73+
* # Examples
74+
* ~~~
75+
* rusti> core::bool::or(true, false)
76+
* true
77+
* ~~~
78+
* rusti> core::bool::or(false, false)
79+
* false
80+
* ~~~
81+
*/
2582
pub fn or(a: bool, b: bool) -> bool { a || b }
2683

2784
/**
28-
* Exclusive or
29-
*
30-
* Identical to `or(and(a, not(b)), and(not(a), b))`
31-
*/
85+
* An 'exclusive or' of two boolean values.
86+
*
87+
* 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
88+
*
89+
* # Examples
90+
* ~~~
91+
* rusti> core::bool::xor(true, false)
92+
* true
93+
* ~~~
94+
* rusti> core::bool::xor(true, true)
95+
* false
96+
* ~~~
97+
*/
3298
pub fn xor(a: bool, b: bool) -> bool { (a && !b) || (!a && b) }
3399

34-
/// Implication in the logic, i.e. from `a` follows `b`
100+
/**
101+
* Implication between two boolean values.
102+
*
103+
* Implication is often phrased as 'if a then b.'
104+
*
105+
* 'if a then b' is equivalent to `!a || b`.
106+
*
107+
* # Examples
108+
* ~~~
109+
* rusti> core::bool::implies(true, true)
110+
* true
111+
* ~~~
112+
* rusti> core::bool::implies(true, false)
113+
* false
114+
* ~~~
115+
*/
35116
pub fn implies(a: bool, b: bool) -> bool { !a || b }
36117

37-
/// true if truth values `a` and `b` are indistinguishable in the logic
118+
/**
119+
* Equality between two boolean values.
120+
*
121+
* Two booleans are equal if they have the same value.
122+
*
123+
* # Examples
124+
* ~~~
125+
* rusti> core::bool::eq(false, true)
126+
* false
127+
* ~~~
128+
* rusti> core::bool::eq(false, false)
129+
* true
130+
* ~~~
131+
*/
38132
pub fn eq(a: bool, b: bool) -> bool { a == b }
39133

40-
/// true if truth values `a` and `b` are distinguishable in the logic
134+
/**
135+
* Non-equality between two boolean values.
136+
*
137+
* Two booleans are not equal if they have different values.
138+
*
139+
* # Examples
140+
* ~~~
141+
* rusti> core::bool::ne(false, true)
142+
* true
143+
* ~~~
144+
* rusti> core::bool::ne(false, false)
145+
* false
146+
* ~~~
147+
*/
41148
pub fn ne(a: bool, b: bool) -> bool { a != b }
42149

43-
/// true if `v` represents truth in the logic
150+
/**
151+
* Is a given boolean value true?
152+
*
153+
* # Examples
154+
* ~~~
155+
* rusti> core::bool::is_true(true)
156+
* true
157+
* ~~~
158+
* rusti> core::bool::is_true(false)
159+
* false
160+
* ~~~
161+
*/
44162
pub fn is_true(v: bool) -> bool { v }
45163

46-
/// true if `v` represents falsehood in the logic
164+
/**
165+
* Is a given boolean value false?
166+
*
167+
* # Examples
168+
* ~~~
169+
* rusti> core::bool::is_false(false)
170+
* true
171+
* ~~~
172+
* rusti> core::bool::is_false(true)
173+
* false
174+
* ~~~
175+
*/
47176
pub fn is_false(v: bool) -> bool { !v }
48177

49-
/// Parse logic value from `s`
178+
/**
179+
* Parse a `bool` from a `str`.
180+
*
181+
* Yields an `Option<bool>`, because `str` may or may not actually be parseable.
182+
*
183+
* # Examples
184+
* ~~~
185+
* rusti> FromStr::from_str::<bool>("true")
186+
* Some(true)
187+
* ~~~
188+
* rusti> FromStr::from_str::<bool>("false")
189+
* Some(false)
190+
* ~~~
191+
* rusti> FromStr::from_str::<bool>("not even a boolean")
192+
* None
193+
* ~~~
194+
*/
50195
impl FromStr for bool {
51196
fn from_str(s: &str) -> Option<bool> {
52197
match s {
@@ -57,19 +202,49 @@ impl FromStr for bool {
57202
}
58203
}
59204

60-
/// Convert `v` into a string
205+
/**
206+
* Convert a `bool` to a `str`.
207+
*
208+
* # Examples
209+
* ~~~
210+
* rusti> std::bool::to_str(true)
211+
* "true"
212+
* ~~~
213+
* rusti> std::bool::to_str(false)
214+
* "false"
215+
* ~~~
216+
*/
61217
pub fn to_str(v: bool) -> ~str { if v { ~"true" } else { ~"false" } }
62218

63219
/**
64-
* Iterates over all truth values by passing them to `blk` in an unspecified
65-
* order
66-
*/
220+
* Iterates over all truth values, passing them to the given block.
221+
*
222+
* There are no guarantees about the order values will be given.
223+
*
224+
* # Examples
225+
* ~~~
226+
* do core::bool::all_values |x: bool| {
227+
* println(core::bool::to_str(x));
228+
* }
229+
* ~~~
230+
*/
67231
pub fn all_values(blk: &fn(v: bool)) {
68232
blk(true);
69233
blk(false);
70234
}
71235

72-
/// converts truth value to an 8 bit byte
236+
/**
237+
* Convert a `bool` to a `u8`.
238+
*
239+
* # Examples
240+
* ~~~
241+
* rusti> std::bool::to_bit(true)
242+
* 1
243+
* ~~~
244+
* rusti> std::bool::to_bit(false)
245+
* 0
246+
* ~~~
247+
*/
73248
#[inline(always)]
74249
pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
75250

0 commit comments

Comments
 (0)