8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
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
+ */
12
36
13
37
#[ cfg( not( test) ) ]
14
38
use cmp:: { Eq , Ord , TotalOrd , Ordering } ;
15
39
use option:: { None , Option , Some } ;
16
40
use from_str:: FromStr ;
17
41
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
+ */
19
54
pub fn not ( v : bool ) -> bool { !v }
20
55
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
+ */
22
68
pub fn and ( a : bool , b : bool ) -> bool { a && b }
23
69
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
+ */
25
82
pub fn or ( a : bool , b : bool ) -> bool { a || b }
26
83
27
84
/**
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
+ */
32
98
pub fn xor ( a : bool , b : bool ) -> bool { ( a && !b) || ( !a && b) }
33
99
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
+ */
35
116
pub fn implies ( a : bool , b : bool ) -> bool { !a || b }
36
117
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
+ */
38
132
pub fn eq ( a : bool , b : bool ) -> bool { a == b }
39
133
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
+ */
41
148
pub fn ne ( a : bool , b : bool ) -> bool { a != b }
42
149
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
+ */
44
162
pub fn is_true ( v : bool ) -> bool { v }
45
163
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
+ */
47
176
pub fn is_false ( v : bool ) -> bool { !v }
48
177
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
+ */
50
195
impl FromStr for bool {
51
196
fn from_str ( s : & str ) -> Option < bool > {
52
197
match s {
@@ -57,19 +202,49 @@ impl FromStr for bool {
57
202
}
58
203
}
59
204
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
+ */
61
217
pub fn to_str ( v : bool ) -> ~str { if v { ~"true " } else { ~"false " } }
62
218
63
219
/**
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
+ */
67
231
pub fn all_values ( blk : & fn ( v : bool ) ) {
68
232
blk ( true ) ;
69
233
blk ( false ) ;
70
234
}
71
235
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
+ */
73
248
#[ inline( always) ]
74
249
pub fn to_bit ( v : bool ) -> u8 { if v { 1u8 } else { 0u8 } }
75
250
0 commit comments