The bool
module contains useful code to help work with boolean values.
A quick summary:
bool
Implementations of the following traits:
FromStr
Ord
TotalOrd
Eq
bool
sAll of the standard comparison functions one would expect: and
, eq
, or
, and more.
Also, a few conversion functions: to_bit
and to_str
.
Finally, some inquries into the nature of truth: is_true
and is_false
.
of FromStr for bool
- Parse a bool
from a str
.of ToStr for bool
- Convert a bool
to a str
.of Ord for bool
of TotalOrd for bool
of Eq for bool
- Equality between two boolean values.all_values
- Iterates over all truth values, passing them to the given block.and
- Conjunction of two boolean values.implies
- Implication between two boolean values.is_false
- Is a given boolean value false?is_true
- Is a given boolean value true?not
- Negation of a boolean value.or
- Disjunction of two boolean values.to_bit
- Convert a bool
to a u8
.xor
- An 'exclusive or' of two boolean values.FromStr
for bool
Parse a bool
from a str
.
Yields an Option<bool>
, because str
may or may not actually be parseable.
rusti> FromStr::from_str::<bool>("true")
Some(true)
rusti> FromStr::from_str::<bool>("false")
Some(false)
rusti> FromStr::from_str::<bool>("not even a boolean")
None
from_str
fn from_str(s: &str) -> Option<bool>
ToStr
for bool
Convert a bool
to a str
.
rusti> true.to_str()
"true"
rusti> false.to_str()
"false"
to_str
fn to_str(&self) -> ~str
Ord
for bool
lt
fn lt(&self, other: &bool) -> bool
le
fn le(&self, other: &bool) -> bool
gt
fn gt(&self, other: &bool) -> bool
ge
fn ge(&self, other: &bool) -> bool
TotalOrd
for bool
cmp
fn cmp(&self, other: &bool) -> Ordering
Eq
for bool
Equality between two boolean values.
Two booleans are equal if they have the same value.
rusti> false.eq(&true)
false
rusti> false == false
true
rusti> false != true
true
rusti> false.ne(&false)
false
eq
fn eq(&self, other: &bool) -> bool
ne
fn ne(&self, other: &bool) -> bool
all_values
fn all_values(blk: &fn(v: bool))
Iterates over all truth values, passing them to the given block.
There are no guarantees about the order values will be given.
do std::bool::all_values |x: bool| {
println(x.to_str())
}
and
fn and(a: bool, b: bool) -> bool
Conjunction of two boolean values.
rusti> std::bool::and(true, false)
false
rusti> std::bool::and(true, true)
true
implies
fn implies(a: bool, b: bool) -> bool
Implication between two boolean values.
Implication is often phrased as 'if a then b.'
'if a then b' is equivalent to !a || b
.
~ {.rust} rusti> std::bool::implies(true, true) true
rusti> std::bool::implies(true, false)
false
is_false
fn is_false(v: bool) -> bool
Is a given boolean value false?
rusti> std::bool::is_false(false)
true
rusti> std::bool::is_false(true)
false
is_true
fn is_true(v: bool) -> bool
Is a given boolean value true?
rusti> std::bool::is_true(true)
true
rusti> std::bool::is_true(false)
false
not
fn not(v: bool) -> bool
Negation of a boolean value.
rusti> std::bool::not(true)
false
rusti> std::bool::not(false)
true
or
fn or(a: bool, b: bool) -> bool
Disjunction of two boolean values.
rusti> std::bool::or(true, false)
true
rusti> std::bool::or(false, false)
false
to_bit
fn to_bit(v: bool) -> u8
Convert a bool
to a u8
.
rusti> std::bool::to_bit(true)
1
rusti> std::bool::to_bit(false)
0
xor
fn xor(a: bool, b: bool) -> bool
An 'exclusive or' of two boolean values.
'exclusive or' is identical to or(and(a, not(b)), and(not(a), b))
.
rusti> std::bool::xor(true, false)
true
rusti> std::bool::xor(true, true)
false