core/
unit.rs

1/// Collapses all unit items from an iterator into one.
2///
3/// This is more useful when combined with higher-level abstractions, like
4/// collecting to a `Result<(), E>` where you only care about errors:
5///
6/// ```
7/// use std::io::*;
8/// let data = vec![1, 2, 3, 4, 5];
9/// let res: Result<()> = data.iter()
10///     .map(|x| writeln!(stdout(), "{x}"))
11///     .collect();
12/// assert!(res.is_ok());
13/// ```
14#[stable(feature = "unit_from_iter", since = "1.23.0")]
15impl FromIterator<()> for () {
16    fn from_iter<I: IntoIterator<Item = ()>>(iter: I) -> Self {
17        iter.into_iter().for_each(|()| {})
18    }
19}
20
21pub(crate) trait IsUnit {
22    fn is_unit() -> bool;
23}
24
25impl<T: ?Sized> IsUnit for T {
26    default fn is_unit() -> bool {
27        false
28    }
29}
30
31impl IsUnit for () {
32    fn is_unit() -> bool {
33        true
34    }
35}