On this page
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 Clone for ()
fn clone(&self) -> Self
fn clone_from(&mut self, source: &Self)
source
. Read more
impl Debug for ()
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl Default for ()
fn default()
Returns the default value of ()
impl Extend<()> for ()
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = ()>,
fn extend_one(&mut self, _item: ())
extend_one
#72631)
fn extend_reserve(&mut self, additional: usize)
extend_one
#72631)
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());
fn from_iter<I>(iter: I)
where
I: IntoIterator<Item = ()>,
impl Hash for ()
fn hash<H>(&self, _state: &mut H)
where
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H)
where
H: Hasher,
Self: Sized,
impl Ord for ()
fn cmp(&self, _other: &()) -> Ordering
fn max(self, other: Self) -> Self
where
Self: Sized,
fn min(self, other: Self) -> Self
where
Self: Sized,
fn clamp(self, min: Self, max: Self) -> Self
where
Self: Sized + PartialOrd,
impl PartialEq for ()
fn eq(&self, _other: &()) -> bool
self
and other
values to be equal, and is used by ==
.
fn ne(&self, _other: &()) -> bool
!=
. The default implementation is almost always sufficient, and should not be overridden without very good reason.
impl PartialOrd for ()
fn partial_cmp(&self, _: &()) -> Option<Ordering>
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
self
and other
) and is used by the >=
operator. Read more
impl Termination for ()
fn report(self) -> ExitCode
impl ConstParamTy for ()
impl Copy for ()
impl Eq for ()
impl StructuralEq for ()
impl StructuralPartialEq for ()
Auto Trait Implementations
impl RefUnwindSafe for ()
impl Send for ()
impl Sync for ()
impl Unpin for ()
impl UnwindSafe for ()
Blanket Implementations
impl<T> Any for T
where
T: 'static + ?Sized,
impl<T> Borrow<T> for T
where
T: ?Sized,
impl<T> BorrowMut<T> for T
where
T: ?Sized,
impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for T
where
U: From<T>,
fn into(self) -> U
Calls U::from(self)
.
That is, this conversion is whatever the implementation of From<T> for U
chooses to do.
impl<T> ToOwned for T
where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)
impl<T, U> TryFrom<U> for T
where
U: Into<T>,
type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for T
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
© 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