Primitive Type unit
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() {}The 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();
};Trait Implementations
impl Debug for ()
pub fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
Formats the value using the given formatter. Read more
pub fn default()
Returns the default value of ()
impl Extend<()> for ()
pub fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = ()>,
Extends a collection with the contents of an iterator. Read more
pub fn extend_one(&mut self, _item: ())
Extends a collection with exactly one element.
fn extend_reserve(&mut self, additional: usize)
Reserves capacity in a collection for the given number of additional elements. Read more
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());pub fn from_iter<I>(iter: I) where
I: IntoIterator<Item = ()>,
Creates a value from an iterator. Read more
impl Hash for ()
impl Ord for ()
pub fn cmp(&self, _other: &()) -> Ordering
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
fn clamp(self, min: Self, max: Self) -> Self
Restrict a value to a certain interval. Read more
impl PartialEq<()> for ()
pub fn eq(&self, _other: &()) -> bool
This method tests for self and other values to be equal, and is used by ==. Read more
pub fn ne(&self, _other: &()) -> bool
This method tests for !=.
impl PartialOrd<()> for ()
pub fn partial_cmp(&self, &()) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
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 more
fn gt(&self, other: &Rhs) -> bool
This method tests greater than (for self and other) and is used by the > operator. Read more
fn ge(&self, other: &Rhs) -> bool
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
impl Termination for ()
fn report(self) -> i32
Is called to get the representation of the value as status code. This status code is returned to the operating system. Read more
impl Eq for ()
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/primitive.unit.html