Expand description
The () type, also called “unit”.
The () type has exactly one value (), and is used when there
is no other meaningful value that could be returned. () is most
commonly seen implicitly: functions without a -> ... implicitly
have return type (), that is, these are equivalent:
fn long() -> () {}
fn short() {}RunThe semicolon ; can be used to discard the result of an
expression at the end of a block, making the expression (and thus
the block) evaluate to (). For example,
fn returns_i64() -> i64 {
1i64
}
fn returns_unit() {
1i64;
}
let is_i64 = {
returns_i64()
};
let is_unit = {
returns_i64();
};RunTrait Implementations
1.28.0 · sourceimpl Extend<()> for ()
impl Extend<()> for ()
sourcefn extend<T: IntoIterator<Item = ()>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = ()>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
sourcefn extend_one(&mut self, _item: ())
fn extend_one(&mut self, _item: ())
🔬This is a nightly-only experimental API. (
extend_one #72631)Extends a collection with exactly one element.
sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one #72631)Reserves capacity in a collection for the given number of additional elements. Read more
1.23.0 · sourceimpl FromIterator<()> for ()
impl FromIterator<()> for ()
Collapses all unit items from an iterator into one.
This is more useful when combined with higher-level abstractions, like
collecting to a Result<(), E> where you only care about errors:
use std::io::*;
let data = vec![1, 2, 3, 4, 5];
let res: Result<()> = data.iter()
.map(|x| writeln!(stdout(), "{x}"))
.collect();
assert!(res.is_ok());Runsourcefn from_iter<I: IntoIterator<Item = ()>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = ()>>(iter: I) -> Self
Creates a value from an iterator. Read more
const: unstable · sourceimpl Ord for ()
impl Ord for ()
1.21.0 · sourceconst fn max(self, other: Self) -> Selfwhere
Self: Sized,
const fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
1.21.0 · sourceconst fn min(self, other: Self) -> Selfwhere
Self: Sized,
const fn min(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the minimum of two values. Read more
1.50.0 · sourceconst fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized,
Self: PartialOrd,
const fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized,
Self: PartialOrd,
Restrict a value to a certain interval. Read more
const: unstable · sourceimpl PartialEq<()> for ()
impl PartialEq<()> for ()
const: unstable · sourceimpl PartialOrd<()> for ()
impl PartialOrd<()> for ()
sourceconst fn le(&self, other: &Rhs) -> bool
const fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self and other) and is used by the <=
operator. Read moreimpl Copy for ()
impl Eq for ()
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more