Primitive Type unit
1.0.0 ·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 · source§impl Extend<()> for ()
impl Extend<()> for ()
1.23.0 · source§impl 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());
Runsource§impl Ord for ()
impl Ord for ()
source§impl PartialOrd<()> for ()
impl PartialOrd<()> for ()
1.61.0 · source§impl Termination for ()
impl Termination for ()
impl Copy for ()
impl Eq for ()
Auto Trait Implementations§
impl RefUnwindSafe for ()
impl Send for ()
impl Sync for ()
impl Unpin for ()
impl UnwindSafe for ()
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more