pub trait WeakTrue {
// Required method
fn weak_true(&self) -> bool;
// Provided methods
fn weak_false(&self) -> bool { ... }
fn weak_then<F, R>(&self, f: F) -> Option<R>
where F: FnOnce() -> R,
Self: Sized { ... }
fn weak_else<F, R>(&self, f: F) -> Option<R>
where F: FnOnce() -> R,
Self: Sized { ... }
}
Expand description
Similar to the automatic implicit conversion to boolean values in weakly typed languages
type | impl |
---|---|
float | self is not 0.0 / NaN |
integer | self != 0 |
reference / smart pointer | inner value impl |
raw pointer | !self.is_null |
Option | self.is_some |
Result | self.is_ok |
Poll | self.is_ready |
str / slice / array | !self.is_empty |
collections | !self.is_empty |
unit | false |
bool | self |
fn / tuple / char | true |
Required Methods§
Sourcefn weak_true(&self) -> bool
fn weak_true(&self) -> bool
Similar to the automatic implicit conversion to boolean values in weakly typed languages
§Examples
assert!("c".weak_true());
assert!('c'.weak_true());
assert!('\0'.weak_true());
assert!([0].weak_true());
assert!((&0 as *const i32).weak_true());
assert!(Some(0).weak_true());
assert!(f64::NAN.weak_false());
assert!(0.0.weak_false());
assert!(0.weak_false());
assert!("".weak_false());
assert!([0; 0].weak_false());
Refer to the documentation on WeakTrue
Provided Methods§
Sourcefn weak_false(&self) -> bool
fn weak_false(&self) -> bool
Default implementation is weak_true
inversion
Sourcefn weak_then<F, R>(&self, f: F) -> Option<R>
fn weak_then<F, R>(&self, f: F) -> Option<R>
Run bool::then
on WeakTrue::weak_true
§Examples
assert_eq!(1.weak_then(|| "a"), Some("a"));
assert_eq!(0.weak_then(|| "a"), None);
Sourcefn weak_else<F, R>(&self, f: F) -> Option<R>
fn weak_else<F, R>(&self, f: F) -> Option<R>
Run bool::then
on WeakTrue::weak_false
§Examples
assert_eq!(1.weak_else(|| "a"), None);
assert_eq!(0.weak_else(|| "a"), Some("a"));