unit - Rust (original) (raw)

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() {}

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();
};

1.0.0 · Source§

1.0.0 · Source§

Source§

Returns the default value of ()

1.28.0 · Source§

Source§

Extends a collection with the contents of an iterator. Read more

Source§

🔬This is a nightly-only experimental API. (extend_one #72631)

Extends a collection with exactly one element.

Source§

🔬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 · Source§

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());

1.0.0 · Source§

1.0.0 · Source§

1.0.0 · Source§

Source§

Tests for self and other values to be equal, and is used by ==.

Source§

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

1.0.0 · Source§

Source§

This method returns an ordering between self and other values if one exists. Read more

1.0.0 · Source§

Tests less than (for self and other) and is used by the < operator. Read more

1.0.0 · Source§

Tests less than or equal to (for self and other) and is used by the<= operator. Read more

1.0.0 · Source§

Tests greater than (for self and other) and is used by the >operator. Read more

1.0.0 · Source§

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more

1.61.0 · Source§

Source§

Is called to get the representation of the value as status code. This status code is returned to the operating system.

Source§

1.0.0 · Source§

Source§

Source§

§

§

§

§

§

§